<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/blog/lib/pace/pace-theme-loading-bar.min.css">
  <script src="/blog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"xiaobai-123.gitee.io","root":"/blog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="mybatisplus的入门案例，整合springboot。">
<meta property="og:type" content="article">
<meta property="og:title" content="MybatisPlus入门">
<meta property="og:url" content="http://xiaobai-123.gitee.io/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="小白个人博客">
<meta property="og:description" content="mybatisplus的入门案例，整合springboot。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://xiaobai-123.gitee.io/images/images/blogImage/2021/08/09/360%E6%88%AA%E5%9B%BE20200612091710348.jpg">
<meta property="og:image" content="https://xiaobai-123.gitee.io/images/images/blogImage/2021/08/09/360%E6%88%AA%E5%9B%BE20200612111205189.jpg">
<meta property="article:published_time" content="2020-08-09T13:20:18.000Z">
<meta property="article:modified_time" content="2021-08-09T13:31:37.413Z">
<meta property="article:author" content="小白">
<meta property="article:tag" content="mybayisplus">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://xiaobai-123.gitee.io/images/images/blogImage/2021/08/09/360%E6%88%AA%E5%9B%BE20200612091710348.jpg">

<link rel="canonical" href="http://xiaobai-123.gitee.io/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>MybatisPlus入门 | 小白个人博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>
<!-- 动态背景 -->
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/blog/js/src/clicklove.js"></script>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">小白个人博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">www.xiaobai.com</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/blog/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>
	<a target="_blank" rel="noopener" href="https://baidu.com" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaobai-123.gitee.io/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="http://xiaobai-123.gitee.io/images/images/1.jpg">
      <meta itemprop="name" content="小白">
      <meta itemprop="description" content="欢迎观看">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小白个人博客">
    </span>
      <header class="post-header">
	    
		  <i class='fa fa-thumb-tack'></i>
		  <font color=7D26CD style='cursor: pointer'>置顶</font>
		
        <h1 class="post-title" itemprop="name headline">
          MybatisPlus入门
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-09 21:20:18" itemprop="dateCreated datePublished" datetime="2020-08-09T21:20:18+08:00">2020-08-09</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-08-09 21:31:37" itemprop="dateModified" datetime="2021-08-09T21:31:37+08:00">2021-08-09</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">后端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <div class="post-description">mybatisplus的入门案例，整合springboot。</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&gt;mybatispuls的入门案例，整合springboot。</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="Mybatis-plus"><a href="#Mybatis-plus" class="headerlink" title="Mybatis plus"></a>Mybatis plus</h3><p>官网<a target="_blank" rel="noopener" href="https://mp.baomidou.com/">https://mp.baomidou.com/</a></p>
<h3 id="基本配置"><a href="#基本配置" class="headerlink" title="基本配置"></a>基本配置</h3><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置日志 默认控制台 </span></span><br><span class="line"><span class="meta">mybatis-plus.configuration.log-impl</span>=<span class="string">org.apache.ibatis.logging.stdout.StdOutImpl</span></span><br><span class="line"><span class="comment"># 配置逻辑删除 </span></span><br><span class="line"><span class="comment"># 逻辑已删除值(默认为 1)</span></span><br><span class="line"><span class="meta">mybatis-plus.global-config.db-config.logic-delete-value</span>=<span class="string">1 </span></span><br><span class="line"><span class="comment"># 逻辑未删除值(默认为 0)</span></span><br><span class="line"><span class="meta">mybatis-plus.global-config.db-config.logic-not-delete-value</span>=<span class="string">0 </span></span><br><span class="line"><span class="meta">mybatis-plus.mapper-locations</span>=<span class="string">classpath:mybatis/mapper/*Mapper.xml</span></span><br><span class="line"><span class="comment">#是否使用 驼峰 默认使用</span></span><br><span class="line"><span class="comment"># mybatis-plus.configuration.map-underscore-to-camel-case=false</span></span><br><span class="line"><span class="comment"># 取别名</span></span><br><span class="line"><span class="meta">mybatis-plus.type-aliases-package</span>=<span class="string">com.myh.pojo</span></span><br></pre></td></tr></table></figure>



<h3 id="ORM映射关系"><a href="#ORM映射关系" class="headerlink" title="ORM映射关系"></a>ORM映射关系</h3><p>java类                                                    mysql数据库<br>java.lang.Byte    byte                          TINYINT<br>java.lang.Short    short                      SMALLINT<br>java.lang.Integer    integer                INGEGER<br>java.lang.Long    long                        BIGINT<br>java.lang.Float    float                        FLOAT<br>java.lang.Double    double                DOUBLE<br>java.lang.BigDecimal    big_decimal    NUMERIC<br>java.lang.Boolean    boolean    BIT<br>java.lang.String    string    VARCHAR<br>java.lang.Boolean    yes_no    CHAR(1)(‘Y’或’N’)<br>java.lang.Boolean    true_false    CHAR(1)(‘Y’或’N’)<br>java.uitl.Date / java.sql.Date    date    DATE<br>java.sql.Time    time    TIME<br>java.sql.Timestamp    timestamp    TIMESTAMP<br>java.uitl.Calendar    celendar    TIMESTAMP<br>java.uitl.Calendar    calendar    TIMESTAMP<br>java.io.Serializable    serializable    VARBINARY/BLOB<br>java.sql.Clob    clob    CLOB<br>java.sql.Blob    blob    BLOB<br>java.lang.Class    class    VARCHAR<br>java.uitl.Locale    locale    VARCHAR<br>java.uitl.TimeZone    timezone    VARCHAR<br>java.uitl.Currency    currency    VARCHAR</p>
<h3 id="target作用范围"><a href="#target作用范围" class="headerlink" title="target作用范围"></a>target作用范围</h3><p><strong>@Target(ElementType.TYPE)——接口、类、枚举、注解<br>@Target(ElementType.FIELD)——字段、枚举的常量<br>@Target(ElementType.METHOD)——方法<br>@Target(ElementType.PARAMETER)——方法参数<br>@Target(ElementType.CONSTRUCTOR) ——构造函数<br>@Target(ElementType.LOCAL_VARIABLE)——局部变量<br>@Target(ElementType.ANNOTATION_TYPE)——注解<br>@Target(ElementType.PACKAGE)——包</strong></p>
<p>@Retention：注解的保留位置</p>
<p>RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。<br>RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。<br>RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。<br>@Document：说明该注解将被包含在javadoc中<br>@Inherited：说明子类可以继承父类中的该注解</p>
<h3 id="快速开始"><a href="#快速开始" class="headerlink" title="快速开始"></a>快速开始</h3><p>创建springboot的链接 eclipse有时候超时  可以用下面这个</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//start.aliyun.com/</span></span><br></pre></td></tr></table></figure>



<p>1.创建数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database mybats_plus charactor <span class="keyword">set</span> utf8</span><br></pre></td></tr></table></figure>

<p>2.创建user表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> <span class="keyword">user</span>;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">user</span></span><br><span class="line">(</span><br><span class="line">	id <span class="type">BIGINT</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;主键ID&#x27;</span>,</span><br><span class="line">	name <span class="type">VARCHAR</span>(<span class="number">30</span>) <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">	age <span class="type">INT</span>(<span class="number">11</span>) <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;年龄&#x27;</span>,</span><br><span class="line">	email <span class="type">VARCHAR</span>(<span class="number">50</span>) <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span> COMMENT <span class="string">&#x27;邮箱&#x27;</span>,</span><br><span class="line">	<span class="keyword">PRIMARY</span> KEY (id)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>真实开发</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>version 乐观锁</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span><span class="keyword">delete</span> 逻辑删除</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>gmt_create</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>gmt_modified</span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> <span class="keyword">user</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> <span class="keyword">user</span> (id, name, age, email) <span class="keyword">VALUES</span></span><br><span class="line">(<span class="number">1</span>, <span class="string">&#x27;Jone&#x27;</span>, <span class="number">18</span>, <span class="string">&#x27;test1@baomidou.com&#x27;</span>),</span><br><span class="line">(<span class="number">2</span>, <span class="string">&#x27;Jack&#x27;</span>, <span class="number">20</span>, <span class="string">&#x27;test2@baomidou.com&#x27;</span>),</span><br><span class="line">(<span class="number">3</span>, <span class="string">&#x27;Tom&#x27;</span>, <span class="number">28</span>, <span class="string">&#x27;test3@baomidou.com&#x27;</span>),</span><br><span class="line">(<span class="number">4</span>, <span class="string">&#x27;Sandy&#x27;</span>, <span class="number">21</span>, <span class="string">&#x27;test4@baomidou.com&#x27;</span>),</span><br><span class="line">(<span class="number">5</span>, <span class="string">&#x27;Billie&#x27;</span>, <span class="number">24</span>, <span class="string">&#x27;test5@baomidou.com&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!-- 数据库 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- mybatis-plus 自己开发的--&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.baomidou<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.5<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-devtools<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>mybatis和mybatisplus不要同时导入  会有依赖版本问题 导入mybatisplus就行了</p>
<p>配置文件</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">5和8有区别</span>  <span class="string">8需要时区 5驱动不同</span></span><br><span class="line"><span class="meta">server.port</span>=<span class="string">8080</span></span><br><span class="line"><span class="meta">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">spring.datasource.password</span>=<span class="string">123456</span></span><br><span class="line"><span class="meta">spring.datasource.url</span>=<span class="string">jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8</span></span><br><span class="line"><span class="meta">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.cj.jdbc.Driver</span></span><br></pre></td></tr></table></figure>

<p>传统方式pojo -dao(连接mybatispuls ，配置mapper.xml文件) -service -controller</p>
<p>现在方式：pojo mapper 使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="meta">@Mapper</span> <span class="comment">//在启动类中取扫描</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line">	<span class="comment">//所有的crud操作都已经完成编写了</span></span><br><span class="line">	<span class="comment">//你不需要像以前配置一大堆文件了</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Test</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mybatisplus01QuickstartApplicationTests</span> </span>&#123;	</span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UserMapper userMapper;	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">contextLoads</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//参数是一个wapper 条件构造器 </span></span><br><span class="line">		<span class="comment">//查询全部用户</span></span><br><span class="line">		List&lt;User&gt; list= userMapper.selectList(<span class="keyword">null</span>);</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="mybatis-plus的字段映射关系"><a href="#mybatis-plus的字段映射关系" class="headerlink" title="mybatis-plus的字段映射关系"></a>mybatis-plus的字段映射关系</h3><blockquote>
<p>在mybatis-plus中，默认开启驼峰命名</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="meta">@TableName(value = &quot;user&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">	<span class="comment">//对应数据库的主件 uuid 自增id 雪花算法 redis zookeeper</span></span><br><span class="line">	<span class="comment">//默认  全局唯一id  ID_WORKER</span></span><br><span class="line">	<span class="comment">//自增AUTO 数据库也有变成自增</span></span><br><span class="line">	<span class="meta">@TableId(type = IdType.AUTO)</span></span><br><span class="line">    <span class="keyword">private</span> Long id;</span><br><span class="line"><span class="comment">//  private String name;</span></span><br><span class="line">    <span class="keyword">private</span> String userName;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">    <span class="keyword">private</span> String email;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@TableField(fill = FieldFill.INSERT)</span></span><br><span class="line">    <span class="keyword">private</span> Date gmtCreate;</span><br><span class="line">    <span class="meta">@TableField(fill = FieldFill.INSERT_UPDATE)</span></span><br><span class="line">    <span class="keyword">private</span> Date gmt_modified;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//  如果你的字段类型为userName，那么在数据库查询的时候映射的时候</span></span><br><span class="line"><span class="comment">//  默认映射为user_name 如果此时你的数据库字段为userName就会报错</span></span><br><span class="line"><span class="comment">//  也就是他自动将userName转化为 user_name </span></span><br><span class="line"><span class="comment">//  所以此时你的数据库字段应该为 user_name</span></span><br><span class="line"><span class="comment">//  也就是只在俩种情况会映射成功</span></span><br><span class="line"><span class="comment">//1.字段名和数据库一样 映射成功</span></span><br><span class="line"><span class="comment">//2.驼峰命名规则也会成功</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>关闭驼峰命名</p>
</blockquote>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">mybatis-plus</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">configuration</span>:<span class="string"></span></span><br><span class="line"><span class="comment">    # 是否开启自动驼峰命名规则（camel case）映射，即从经典数据库列名 A_COLUMN（下划线命名） 到经典 Java 属性名 aColumn（驼峰命名） 的类似映射</span></span><br><span class="line">    <span class="meta">map-underscore-to-camel-case</span>: <span class="string">false</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>spring中</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- MP 全局配置 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;globalConfig&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baomidou.mybatisplus.entity.GlobalConfiguration&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 全局的主键策略 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;idType&quot;</span> <span class="attr">value</span>=<span class="string">&quot;0&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--映射数据库下划线字段名到数据库实体类的驼峰命名的映射--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dbColumnUnderline&quot;</span> <span class="attr">value</span>=<span class="string">&quot;flase&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>###配置日志</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置日志 默认控制台</span></span><br><span class="line"><span class="meta">mybatis-plus.configuration.log-impl</span>=<span class="string">org.apache.ibatis.logging.stdout.StdOutImpl</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">List&lt;User&gt; list= userMapper.selectList(<span class="keyword">null</span>);</span><br><span class="line">List&lt;User&gt; list1= userMapper.selectList(<span class="keyword">null</span>);</span><br><span class="line"><span class="comment">//没有缓存</span></span><br></pre></td></tr></table></figure>

<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">Creating</span> <span class="string">a new SqlSession</span></span><br><span class="line"><span class="attr">SqlSession</span> <span class="string">[org.apache.ibatis.session.defaults.DefaultSqlSession@655a01d8] was not registered for synchronization because synchronization is not active</span></span><br><span class="line"><span class="attr">JDBC</span> <span class="string">Connection [HikariProxyConnection@935599808 wrapping com.mysql.cj.jdbc.ConnectionImpl@35a0e495] will not be managed by Spring</span></span><br><span class="line">==<span class="meta">&gt;</span>  <span class="string">Preparing: SELECT id,name,age,email FROM user </span></span><br><span class="line">==<span class="meta">&gt;</span> <span class="string">Parameters: </span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=    Columns: id, name, age, email</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 1, Jone, 18, test1@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 2, Jack, 20, test2@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 3, Tom, 28, test3@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 4, Sandy, 21, test4@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 5, Billie, 24, test5@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=      Total: 5</span></span><br><span class="line"><span class="attr">Closing</span> <span class="string">non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@655a01d8]</span></span><br><span class="line"><span class="attr">Creating</span> <span class="string">a new SqlSession</span></span><br><span class="line"><span class="attr">SqlSession</span> <span class="string">[org.apache.ibatis.session.defaults.DefaultSqlSession@770beef5] was not registered for synchronization because synchronization is not active</span></span><br><span class="line"><span class="attr">JDBC</span> <span class="string">Connection [HikariProxyConnection@1842476600 wrapping com.mysql.cj.jdbc.ConnectionImpl@35a0e495] will not be managed by Spring</span></span><br><span class="line">==<span class="meta">&gt;</span>  <span class="string">Preparing: SELECT id,name,age,email FROM user </span></span><br><span class="line">==<span class="meta">&gt;</span> <span class="string">Parameters: </span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=    Columns: id, name, age, email</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 1, Jone, 18, test1@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 2, Jack, 20, test2@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 3, Tom, 28, test3@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 4, Sandy, 21, test4@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=        Row: 5, Billie, 24, test5@baomidou.com</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=      Total: 5</span></span><br><span class="line"><span class="attr">Closing</span> <span class="string">non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@770beef5]</span></span><br><span class="line"><span class="meta">User(id</span>=<span class="string">1, name=Jone, age=18, email=test1@baomidou.com)</span></span><br><span class="line"><span class="meta">User(id</span>=<span class="string">2, name=Jack, age=20, email=test2@baomidou.com)</span></span><br><span class="line"><span class="meta">User(id</span>=<span class="string">3, name=Tom, age=28, email=test3@baomidou.com)</span></span><br><span class="line"><span class="meta">User(id</span>=<span class="string">4, name=Sandy, age=21, email=test4@baomidou.com)</span></span><br><span class="line"><span class="meta">User(id</span>=<span class="string">5, name=Billie, age=24, email=test5@baomidou.com)</span></span><br></pre></td></tr></table></figure>

<h3 id="crud"><a href="#crud" class="headerlink" title="crud"></a>crud</h3><p>####插入</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">contextLoads1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="comment">//参数是一个wapper 条件构造器 </span></span><br><span class="line">	<span class="comment">//查询全部用户</span></span><br><span class="line">	User user=<span class="keyword">new</span> User(); <span class="comment">//自动生成id</span></span><br><span class="line">	user.setAge(<span class="number">3</span>);</span><br><span class="line">	user.setEmail(<span class="string">&quot;1@qq.com&quot;</span>);</span><br><span class="line">	user.setName(<span class="string">&quot;A&quot;</span>);</span><br><span class="line">	userMapper.insert(user); <span class="comment">//返回会影响的行数  id会自动回填</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>控制台</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">Creating</span> <span class="string">a new SqlSession</span></span><br><span class="line"><span class="attr">SqlSession</span> <span class="string">[org.apache.ibatis.session.defaults.DefaultSqlSession@c269425] was not registered for synchronization because synchronization is not active</span></span><br><span class="line"><span class="attr">JDBC</span> <span class="string">Connection [HikariProxyConnection@1818236857 wrapping com.mysql.cj.jdbc.ConnectionImpl@4730e0f0] will not be managed by Spring</span></span><br><span class="line">==<span class="meta">&gt;</span>  <span class="string">Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? ) </span></span><br><span class="line">==<span class="meta">&gt;</span> <span class="string">Parameters: 1264024876485070849(Long), A(String), 3(Integer), 1@qq.com(String)</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=    Updates: 1</span></span><br><span class="line"><span class="attr">Closing</span> <span class="string">non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c269425]</span></span><br></pre></td></tr></table></figure>

<p>主键生成策略</p>
<p>==@TableId(type = IdType.AUTO)==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Getter</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">enum</span> <span class="title">IdType</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 数据库ID自增</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    AUTO(<span class="number">0</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 该类型为未设置主键类型</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    NONE(<span class="number">1</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 用户输入ID</span></span><br><span class="line"><span class="comment">     * 该类型可以通过自己注册自动填充插件进行填充</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    INPUT(<span class="number">2</span>),</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 以下3种类型、只有当插入对象ID 为空，才自动填充。 */</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 全局唯一ID (idWorker)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    ID_WORKER(<span class="number">3</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 全局唯一ID (UUID)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    UUID(<span class="number">4</span>),</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字符串全局唯一ID (idWorker 的字符串表示)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    ID_WORKER_STR(<span class="number">5</span>);</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> key;</span><br><span class="line">    IdType(<span class="keyword">int</span> key) &#123;</span><br><span class="line">        <span class="keyword">this</span>.key = key;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>####主键生成策</p>
<blockquote>
<p>默认 ID_WORKER 全局唯一id</p>
</blockquote>
<p>分布式系统唯一id生成：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/haoxinyue/p/5208136.html">https://www.cnblogs.com/haoxinyue/p/5208136.html</a></p>
<p><strong>雪花算法：</strong></p>
<p>snowﬂake是Twitter开源的分布式ID生成算法，结果是一个long型的ID。其核心思想是：使</p>
<p>用41bit作为 毫秒数，10bit作为机器的ID（5个bit是数据中心，5个bit的机器ID），12bit作</p>
<p>为毫秒内的流水号（意味 着每个节点在每毫秒可以产生 4096 个 ID），后还有一个符号位，</p>
<p>永远是0。可以保证几乎全球唯 一！</p>
<blockquote>
<p>主键自增</p>
</blockquote>
<p>==1、实体类字段上 @TableId(type = IdType.AUTO)==</p>
<p>==2、数据库字段一定要是自增！==</p>
<p>==3、再次测试插入即可！==</p>
<p>####更新</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">contextLoads2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//参数是一个wapper 条件构造器 </span></span><br><span class="line">		<span class="comment">//查询全部用户</span></span><br><span class="line">		User user=<span class="keyword">new</span> User();</span><br><span class="line">		user.setId(<span class="number">1L</span>);</span><br><span class="line">		user.setAge(<span class="number">30</span>);</span><br><span class="line">		user.setEmail(<span class="string">&quot;3@qq.com&quot;</span>);</span><br><span class="line">		user.setName(<span class="string">&quot;3&quot;</span>);</span><br><span class="line">		userMapper.updateById(user);</span><br><span class="line">	&#125;</span><br><span class="line"><span class="comment">// 在插入时 如果对象的字段的属性为null 那么在插入时不会出现在插入语句中</span></span><br></pre></td></tr></table></figure>

<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">Creating</span> <span class="string">a new SqlSession</span></span><br><span class="line"><span class="attr">SqlSession</span> <span class="string">[org.apache.ibatis.session.defaults.DefaultSqlSession@6614bd4b] was not registered for synchronization because synchronization is not active</span></span><br><span class="line"><span class="attr">JDBC</span> <span class="string">Connection [HikariProxyConnection@282812721 wrapping com.mysql.cj.jdbc.ConnectionImpl@6c6017b9] will not be managed by Spring</span></span><br><span class="line">==<span class="meta">&gt;</span>  <span class="string">Preparing: UPDATE user SET name=?, age=?, email=? WHERE id=? </span></span><br><span class="line">==<span class="meta">&gt;</span> <span class="string">Parameters: 3(String), 30(Integer), 3@qq.com(String), 1(Long)</span></span><br><span class="line"><span class="meta">&lt;</span>=<span class="string">=    Updates: 1</span></span><br><span class="line"><span class="attr">Closing</span> <span class="string">non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6614bd4b]</span></span><br></pre></td></tr></table></figure>

<h4 id="自动填充"><a href="#自动填充" class="headerlink" title="自动填充"></a>自动填充</h4><p>创建时间 修改时间 ！这些操作一遍都是自动化完成的，不希望手动更新</p>
<p>阿里巴巴开发手册：所有的数据库表：gmt_create  gmt_modified几乎所有的表都要配置上，查看数据修改时间</p>
<p>新字段</p>
<p>gmt_create  gmt_modified  //CURRENT_TIMESTAMP 默认值 当前时间</p>
<p>==private Data gmtCreate;==<br>==private Data gmtModified==;</p>
<p>方式二</p>
<p>代码级别</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@TableField(fill = FieldFill.UPDATE)</span></span><br><span class="line"><span class="keyword">private</span> Data gmtCreate;</span><br><span class="line"><span class="meta">@TableField(fill = FieldFill.INSERT_UPDATE)</span></span><br><span class="line"><span class="keyword">private</span> Data gmtModified;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">enum</span> <span class="title">FieldFill</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 默认不处理</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    DEFAULT,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 插入填充字段</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    INSERT,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新填充字段</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    UPDATE,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 插入和更新填充字段</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    INSERT_UPDATE</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编写处理器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="meta">@Component</span> <span class="comment">//要把处理器加入到ioc容器中</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Myhander</span> <span class="keyword">implements</span> <span class="title">MetaObjectHandler</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insertFill</span><span class="params">(MetaObject metaObject)</span> </span>&#123;</span><br><span class="line">		log.info(<span class="string">&quot;start insert insertFill&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.setFieldValByName(<span class="string">&quot;gmtCreate&quot;</span>, <span class="keyword">new</span> Date(), metaObject);</span><br><span class="line">		<span class="keyword">this</span>.setFieldValByName(<span class="string">&quot;gmtModified&quot;</span>, <span class="keyword">new</span> Date(), metaObject);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateFill</span><span class="params">(MetaObject metaObject)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		log.info(<span class="string">&quot;start insert updateFill&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.setFieldValByName(<span class="string">&quot;gmtModified&quot;</span>, <span class="keyword">new</span> Date(), metaObject);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="乐观锁"><a href="#乐观锁" class="headerlink" title="乐观锁"></a>乐观锁</h4><blockquote>
<p>乐观锁 : 故名思意十分乐观，它总是认为不会出现问题，无论干什么不去上锁！如果出现了问题， 再次更新值测试</p>
<p>悲观锁：故名思意十分悲观，它总是认为总是出现问题，无论干什么都会上锁！再去操作！</p>
</blockquote>
<p>我们这里主要讲解 乐观锁机制！</p>
<p>乐观锁实现方式：</p>
<ul>
<li>取出记录时，获取当前 version </li>
<li>更新时，带上这个version </li>
<li>执行更新时， set version = newVersion where version = oldVersion </li>
<li>如果version不对，就更新失</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">乐观锁：<span class="number">1</span>、先查询，获得版本号 version <span class="operator">=</span> <span class="number">1</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">-- A update user set name = &quot;kuangshen&quot;, version = version + 1 </span></span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">2</span> <span class="keyword">and</span> version <span class="operator">=</span> <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- B 线程抢先完成，这个时候 version = 2，会导致 A 修改失败！ </span></span><br><span class="line">update <span class="keyword">user</span> <span class="keyword">set</span> name <span class="operator">=</span> &quot;kuangshen&quot;, version <span class="operator">=</span> version <span class="operator">+</span> <span class="number">1</span> </span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">2</span> <span class="keyword">and</span> version <span class="operator">=</span> <span class="number">1</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>测试一下MP的乐观锁插件</p>
</blockquote>
<p>1、给数据库中增加version字段！</p>
<p><img src="https://xiaobai-123.gitee.io/images/images/blogImage/2021/08/09/360%E6%88%AA%E5%9B%BE20200612091710348.jpg"></p>
<p>2、我们实体类加对应的字段</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//乐观锁</span></span><br><span class="line"><span class="meta">@Version</span> <span class="comment">//乐观锁Version注解 </span></span><br><span class="line"><span class="keyword">private</span> Integer version;</span><br></pre></td></tr></table></figure>
<p>3、注册组件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//扫描我们的 mapper 文件夹 </span></span><br><span class="line"><span class="meta">@MapperScan(&quot;com.myh.mapper&quot;)</span></span><br><span class="line"><span class="meta">@EnableTransactionManagement</span> </span><br><span class="line"><span class="comment">//Spring Boot 使用事务非常简单，首先使用注解 @EnableTransactionManagement</span></span><br><span class="line"><span class="comment">// 开启事务支持后，然后在访问数据库的Service方法上添加注解 @Transactional 便可</span></span><br><span class="line"><span class="meta">@Configuration</span> <span class="comment">// 配置</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyMybatisPlusConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 注册乐观锁插件    </span></span><br><span class="line">	<span class="meta">@Bean</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> OptimisticLockerInterceptor <span class="title">optimisticLockerInterceptor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> OptimisticLockerInterceptor();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>4、测试一下！</p>
<p>成功</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更新一个用户  测试乐观锁</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">contextLoads3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//参数是一个wapper 条件构造器 </span></span><br><span class="line">    <span class="comment">//查询全部用户</span></span><br><span class="line">    User user = userMapper.selectById(<span class="number">1L</span>);</span><br><span class="line">    user.setName(<span class="string">&quot;1L&quot;</span>);</span><br><span class="line">    userMapper.updateById(user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>sql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span>  Preparing: UPDATE <span class="keyword">user</span> <span class="keyword">SET</span> name<span class="operator">=</span>?, age<span class="operator">=</span>?, email<span class="operator">=</span>?, gmt_create<span class="operator">=</span>?, gmt_modified<span class="operator">=</span>?, version<span class="operator">=</span>? <span class="keyword">WHERE</span> id<span class="operator">=</span>? <span class="keyword">AND</span> version<span class="operator">=</span>? </span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span> Parameters: <span class="number">1</span>L(String), <span class="number">301</span>(<span class="type">Integer</span>), <span class="number">3</span><span class="variable">@qq</span>.com(String), <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">19</span>:<span class="number">12</span>:<span class="number">05.0</span>(<span class="type">Timestamp</span>), <span class="number">2020</span><span class="number">-06</span><span class="number">-12</span> <span class="number">09</span>:<span class="number">53</span>:<span class="number">10.931</span>(<span class="type">Timestamp</span>), <span class="number">2</span>(<span class="type">Integer</span>), <span class="number">1</span>(Long), <span class="number">1</span>(<span class="type">Integer</span>)</span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>    Updates: <span class="number">1</span></span><br></pre></td></tr></table></figure>

<p>失败</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//更新一个用户  测试乐观锁  失败</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">contextLoads4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 线程 1 </span></span><br><span class="line">    User user = userMapper.selectById(<span class="number">1L</span>);</span><br><span class="line">    user.setName(<span class="string">&quot;1L&quot;</span>);</span><br><span class="line">    <span class="comment">// 模拟另外一个线程执行了插队操作 </span></span><br><span class="line">    User user2 = userMapper.selectById(<span class="number">1L</span>);</span><br><span class="line">    user2.setName(<span class="string">&quot;2L&quot;</span>);</span><br><span class="line">    userMapper.updateById(user2);</span><br><span class="line">    userMapper.updateById(user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>sql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span>  Preparing: UPDATE <span class="keyword">user</span> <span class="keyword">SET</span> name<span class="operator">=</span>?, age<span class="operator">=</span>?, email<span class="operator">=</span>?, gmt_create<span class="operator">=</span>?, gmt_modified<span class="operator">=</span>?, version<span class="operator">=</span>? <span class="keyword">WHERE</span> id<span class="operator">=</span>? <span class="keyword">AND</span> version<span class="operator">=</span>? </span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span> Parameters: <span class="number">111</span>(String), <span class="number">301</span>(<span class="type">Integer</span>), <span class="number">3</span><span class="variable">@qq</span>.com(String), <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">19</span>:<span class="number">12</span>:<span class="number">05.0</span>(<span class="type">Timestamp</span>), <span class="number">2020</span><span class="number">-06</span><span class="number">-12</span> <span class="number">09</span>:<span class="number">56</span>:<span class="number">11.94</span>(<span class="type">Timestamp</span>), <span class="number">3</span>(<span class="type">Integer</span>), <span class="number">1</span>(Long), <span class="number">2</span>(<span class="type">Integer</span>)</span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>    Updates: <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span>  Preparing: UPDATE <span class="keyword">user</span> <span class="keyword">SET</span> name<span class="operator">=</span>?, age<span class="operator">=</span>?, email<span class="operator">=</span>?, gmt_create<span class="operator">=</span>?, gmt_modified<span class="operator">=</span>?, version<span class="operator">=</span>? <span class="keyword">WHERE</span> id<span class="operator">=</span>? <span class="keyword">AND</span> version<span class="operator">=</span>? </span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span> Parameters: <span class="number">2</span>L(String), <span class="number">301</span>(<span class="type">Integer</span>), <span class="number">3</span><span class="variable">@qq</span>.com(String), <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">19</span>:<span class="number">12</span>:<span class="number">05.0</span>(<span class="type">Timestamp</span>), <span class="number">2020</span><span class="number">-06</span><span class="number">-12</span> <span class="number">09</span>:<span class="number">56</span>:<span class="number">12.032</span>(<span class="type">Timestamp</span>), <span class="number">3</span>(<span class="type">Integer</span>), <span class="number">1</span>(Long), <span class="number">2</span>(<span class="type">Integer</span>)</span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>    Updates: <span class="number">0</span></span><br></pre></td></tr></table></figure>

<h4 id="查询操作"><a href="#查询操作" class="headerlink" title="查询操作"></a>查询操作</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查询操作</span></span><br><span class="line"><span class="comment">// 测试查询  查询一个</span></span><br><span class="line"><span class="meta">@Test</span>  </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectById</span><span class="params">()</span></span>&#123; </span><br><span class="line">    User user = userMapper.selectById(<span class="number">1L</span>);</span><br><span class="line">    System.out.println(user); </span><br><span class="line"><span class="comment">//==&gt;  Preparing: SELECT id,name,age,email,gmt_create,gmt_modified,version FROM user WHERE id=? </span></span><br><span class="line"><span class="comment">//==&gt; Parameters: 1(Long)</span></span><br><span class="line"><span class="comment">//&lt;==    Columns: id, name, age, email, gmt_create, gmt_modified, version</span></span><br><span class="line"><span class="comment">//&lt;==        Row: 1, 2L1, 301, 3@qq.com, 2020-05-23 11:12:05, 2020-06-12 02:07:07, 6</span></span><br><span class="line"><span class="comment">//&lt;==      Total: 1</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//批量查询</span></span><br><span class="line"><span class="meta">@Test</span>  </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectByBatchId</span><span class="params">()</span></span>&#123; </span><br><span class="line">    List&lt;User&gt; users = userMapper.selectBatchIds(Arrays.asList(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)); </span><br><span class="line">    users.forEach(System.out::println);</span><br><span class="line"><span class="comment">//==&gt; Parameters: 1(Integer), 2(Integer), 3(Integer)</span></span><br><span class="line"><span class="comment">//&lt;==    Columns: id, name, age, email, gmt_create, gmt_modified, version</span></span><br><span class="line"><span class="comment">//&lt;==        Row: 1, 2L1, 301, 3@qq.com, 2020-05-23 11:12:05, 2020-06-12 02:07:07, 6</span></span><br><span class="line"><span class="comment">//&lt;==        Row: 2, 3, 301, 3@qq.com, 2020-05-23 11:12:05, 2020-06-12 01:38:36, 1</span></span><br><span class="line"><span class="comment">//&lt;==        Row: 3, Tom, 28, test3@baomidou.com, 2020-05-23 11:12:05, 2020-05-23 11:12:38, 1</span></span><br><span class="line"><span class="comment">//&lt;==      Total: 3</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 按条件查询之一使用map操作</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectByBatchIds</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();<span class="comment">// 自定义要查询  </span></span><br><span class="line">    map.put(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;Billie&quot;</span>);</span><br><span class="line">    map.put(<span class="string">&quot;age&quot;</span>, <span class="number">24</span>);</span><br><span class="line">    userMapper.selectByMap(map);</span><br><span class="line"><span class="comment">//==&gt;  Preparing: SELECT id,name,age,email,gmt_create,gmt_modified,version FROM user WHERE name = ? AND age = ? </span></span><br><span class="line"><span class="comment">//==&gt; Parameters: Billie(String), 24(Integer)</span></span><br><span class="line"><span class="comment">//&lt;==    Columns: id, name, age, email, gmt_create, gmt_modified, version</span></span><br><span class="line"><span class="comment">//&lt;==        Row: 5, Billie, 24, test5@baomidou.com, 2020-05-23 11:12:05, 2020-05-23 11:12:38, 1</span></span><br><span class="line"><span class="comment">//&lt;==      Total: 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h4><p>1、原始的 limit 进行分页</p>
<p>2、pageHelper 第三方插件</p>
<p>3、MP 其实也内置了分页插件！</p>
<blockquote>
<p>如何使用？</p>
</blockquote>
<p>1、配置拦截器组件即可</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 分页插件 </span></span><br><span class="line"><span class="meta">@Bean</span> </span><br><span class="line"><span class="function"><span class="keyword">public</span> PaginationInterceptor <span class="title">paginationInterceptor</span><span class="params">()</span> </span>&#123;    </span><br><span class="line">    <span class="keyword">return</span>  <span class="keyword">new</span> PaginationInterceptor(); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 测试分页查询</span></span><br><span class="line"><span class="comment">// 1.注册插件</span></span><br><span class="line"><span class="comment">// 2.new Page</span></span><br><span class="line"><span class="comment">// 3.开始查询</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 按条件查询之一使用map操作</span></span><br><span class="line"><span class="comment">// 测试分页查询</span></span><br><span class="line"><span class="meta">@Test</span> </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testPage</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//  参数一：当前页    </span></span><br><span class="line">    <span class="comment">//  参数二：页面大小    </span></span><br><span class="line">    <span class="comment">//  使用了分页插件之后，所有的分页操作也变得简单的！    </span></span><br><span class="line">    Page&lt;User&gt; page = <span class="keyword">new</span> Page&lt;&gt;(<span class="number">2</span>,<span class="number">5</span>);</span><br><span class="line">    IPage&lt;User&gt; selectPage = userMapper.selectPage(page,<span class="keyword">null</span>);</span><br><span class="line">    System.out.println(selectPage.getCurrent());  </span><br><span class="line">    System.out.println(selectPage.getPages());</span><br><span class="line">    System.out.println(selectPage.getSize());</span><br><span class="line">    System.out.println(selectPage.getTotal());</span><br><span class="line">    page.getRecords().forEach(System.out::println);</span><br><span class="line">    <span class="comment">//	 	System.out.println(page.getTotal()); </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>sql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span>  Preparing: <span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="number">1</span>) <span class="keyword">FROM</span> <span class="keyword">user</span> </span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span> Parameters: </span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>    Columns: <span class="built_in">COUNT</span>(<span class="number">1</span>)</span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>        <span class="type">Row</span>: <span class="number">9</span></span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span>  Preparing: <span class="keyword">SELECT</span> id,name,age,email,gmt_create,gmt_modified,version <span class="keyword">FROM</span> <span class="keyword">user</span> LIMIT <span class="number">5</span>,<span class="number">5</span> </span><br><span class="line"><span class="operator">=</span><span class="operator">=</span><span class="operator">&gt;</span> Parameters: </span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>    Columns: id, name, age, email, gmt_create, gmt_modified, version</span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>        <span class="type">Row</span>: <span class="number">1264024876485070849</span>, A, <span class="number">3</span>, <span class="number">1</span><span class="variable">@qq</span>.com, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">11</span>:<span class="number">12</span>:<span class="number">05</span>, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">11</span>:<span class="number">12</span>:<span class="number">38</span>, <span class="number">1</span></span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>        <span class="type">Row</span>: <span class="number">1264024876485070850</span>, A, <span class="number">3</span>, <span class="number">1</span><span class="variable">@qq</span>.com, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">11</span>:<span class="number">12</span>:<span class="number">05</span>, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">11</span>:<span class="number">12</span>:<span class="number">38</span>, <span class="number">1</span></span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>        <span class="type">Row</span>: <span class="number">1264024876485070851</span>, <span class="number">3</span>, <span class="number">30</span>, <span class="number">3</span><span class="variable">@qq</span>.com, <span class="number">2020</span><span class="number">-06</span><span class="number">-10</span> <span class="number">09</span>:<span class="number">02</span>:<span class="number">20</span>, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">03</span>:<span class="number">33</span>:<span class="number">16</span>, <span class="number">1</span></span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>        <span class="type">Row</span>: <span class="number">1264024876485070852</span>, <span class="number">3</span>, <span class="number">301</span>, <span class="number">3</span><span class="variable">@qq</span>.com, <span class="number">2020</span><span class="number">-05</span><span class="number">-23</span> <span class="number">03</span>:<span class="number">33</span>:<span class="number">34</span>, <span class="number">2020</span><span class="number">-06</span><span class="number">-12</span> <span class="number">01</span>:<span class="number">30</span>:<span class="number">40</span>, <span class="number">1</span></span><br><span class="line"><span class="operator">&lt;=</span><span class="operator">=</span>      Total: <span class="number">4</span></span><br></pre></td></tr></table></figure>

<h4 id="删除操作"><a href="#删除操作" class="headerlink" title="删除操作"></a>删除操作</h4><p>1、根据 id 删除记录</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">	<span class="comment">// 测试删除 </span></span><br><span class="line">	<span class="meta">@Test</span> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDeleteById</span><span class="params">()</span></span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> deleteById = userMapper.deleteById(<span class="number">1240620674645544965L</span>);</span><br><span class="line">		System.out.println(deleteById);</span><br><span class="line"><span class="comment">//==&gt;  Preparing: DELETE FROM user WHERE id=? </span></span><br><span class="line"><span class="comment">//==&gt; Parameters: 1240620674645544965(Long)</span></span><br><span class="line"><span class="comment">//&lt;==    Updates: 0	</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">// 通过id批量删除 </span></span><br><span class="line">	<span class="meta">@Test</span> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDeleteBatchId</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(<span class="number">1264024876485070852L</span>,<span class="number">1240620674645544962L</span>)); </span><br><span class="line">		System.out.println(deleteBatchIds);</span><br><span class="line"><span class="comment">//==&gt;  Preparing: DELETE FROM user WHERE id IN ( ? , ? ) </span></span><br><span class="line"><span class="comment">//==&gt; Parameters: 1264024876485070852(Long), 1240620674645544962(Long)</span></span><br><span class="line"><span class="comment">//&lt;==    Updates: 1</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 通过map删除  动态sql</span></span><br><span class="line">	<span class="meta">@Test</span> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDeleteMap</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(); </span><br><span class="line">		map.put(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;1&quot;</span>);</span><br><span class="line">		map.put(<span class="string">&quot;age&quot;</span>,<span class="string">&quot;1&quot;</span>);</span><br><span class="line">		userMapper.deleteByMap(map);</span><br><span class="line">	&#125;</span><br><span class="line"><span class="comment">//==&gt;  Preparing: DELETE FROM user WHERE name = ? AND age = ? </span></span><br><span class="line"><span class="comment">//==&gt; Parameters: 1(String), 1(String)</span></span><br><span class="line"><span class="comment">//&lt;==    Updates: 0</span></span><br></pre></td></tr></table></figure>

<h4 id="逻辑删除"><a href="#逻辑删除" class="headerlink" title="逻辑删除"></a>逻辑删除</h4><blockquote>
<p>物理删除 ：从数据库中直接移除  </p>
<p>逻辑删除 ：再数据库中没有被移除，而是通过一个变量来让他失效！ deleted = 0 =&gt; deleted = 1</p>
</blockquote>
<p>1、在数据表中增加一个 deleted 字段</p>
<p><img src="https://xiaobai-123.gitee.io/images/images/blogImage/2021/08/09/360%E6%88%AA%E5%9B%BE20200612111205189.jpg"></p>
<p>2、实体类中增加属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@TableLogic</span> <span class="comment">//逻辑删除 </span></span><br><span class="line"><span class="keyword">private</span> Integer deleted;</span><br></pre></td></tr></table></figure>

<p>3、配置插件！</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置逻辑删除 </span></span><br><span class="line"><span class="comment"># 逻辑已删除值(默认为 1)</span></span><br><span class="line"><span class="meta">mybatis-plus.global-config.db-config.logic-delete-value</span>=<span class="string">1 </span></span><br><span class="line"><span class="comment"># 逻辑未删除值(默认为 0)</span></span><br><span class="line"><span class="meta">mybatis-plus.global-config.db-config.logic-not-delete-value</span>=<span class="string">0 </span></span><br></pre></td></tr></table></figure>

<p>4.注册插件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 逻辑删除组件！</span></span><br><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ISqlInjector <span class="title">sqlInjector</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> LogicSqlInjector();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>附件说明</p>
<ul>
<li>逻辑删除是为了方便数据恢复和保护数据本身价值等等的一种方案，但实际就是删除。</li>
<li>如果你需要再查出来就不应使用逻辑删除，而是以一个状态去表示。</li>
</ul>
<p>如： 员工离职，账号被锁定等都应该是一个状态字段，此种场景不应使用逻辑删除。</p>
<ul>
<li>若确需查找删除数据，如老板需要查看历史所有数据的统计汇总信息，请单独手写sql。</li>
</ul>
<h4 id="性能分析插件"><a href="#性能分析插件" class="headerlink" title="性能分析插件"></a>性能分析插件</h4><blockquote>
<p>性能分析拦截器，用于输出每条 SQL 语句及其执行时间</p>
</blockquote>
<blockquote>
<p>该插件 <code>3.2.0</code> 以上版本移除推荐使用第三方扩展 <a target="_blank" rel="noopener" href="https://mp.baomidou.com/guide/p6spy">执行SQL分析打印</a> 功能</p>
</blockquote>
<p>我们在平时的开发中，会遇到一些慢sql。测试！ druid,,,,, </p>
<p>作用：性能分析拦截器，用于输出每条 SQL 语句及其执行时间</p>
<p> MP也提供性能分析插件，如果超过这个时间就停止运行！</p>
<p>1、导入插件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**     * SQL执行效率插件     */</span> </span><br><span class="line"><span class="comment">// 设置 dev test 环境开启，保证我们的效率 </span></span><br><span class="line"><span class="meta">@Bean</span> </span><br><span class="line"><span class="meta">@Profile(&#123;&quot;dev&quot;,&quot;test&quot;&#125;)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> PerformanceInterceptor <span class="title">performanceInterceptor</span><span class="params">()</span> </span>&#123; </span><br><span class="line">    PerformanceInterceptor performanceInterceptor = <span class="keyword">new</span> PerformanceInterceptor();</span><br><span class="line">    <span class="comment">// ms设置sql执行的最大时间，如果超过了则不 执行1000ms   </span></span><br><span class="line">    performanceInterceptor.setMaxTime(<span class="number">10</span>); </span><br><span class="line">    <span class="comment">//		performanceInterceptor.setWriteInLog(true); //是否写入日志</span></span><br><span class="line">    <span class="comment">// 是否格式化代码    </span></span><br><span class="line">    performanceInterceptor.setFormat(<span class="keyword">true</span>);  </span><br><span class="line">    <span class="keyword">return</span> performanceInterceptor; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>记住，要在SpringBoot中配置环境为dev或者 test 环境！</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">spring.profiles.active</span>=<span class="string">dev</span></span><br></pre></td></tr></table></figure>

<p>2、测试使用！</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查询操作</span></span><br><span class="line"><span class="comment">// 测试查询  查询一个</span></span><br><span class="line"><span class="meta">@Test</span>  </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectById</span><span class="params">()</span></span>&#123; </span><br><span class="line">    User user = userMapper.selectById(<span class="number">2L</span>);</span><br><span class="line">    System.out.println(user); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="多表-分页-模糊-自定义SQL"><a href="#多表-分页-模糊-自定义SQL" class="headerlink" title="多表 分页 模糊 自定义SQL"></a>多表 分页 模糊 自定义SQL</h3><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/chaoba/p/10434239.html">https://www.cnblogs.com/chaoba/p/10434239.html</a> 分页多表链接</p>
<blockquote>
<p>自定义sql</p>
<p>1.注解</p>
<p>2.和mybatis一样的配置文件</p>
<p>3.可以将mybatis-plsu 看成mybatis</p>
<p>原因是只在mybatis上面只做增强 不做修改 </p>
<p>但是多表还是要自己写sql，虽然自己写 但是模糊和分页已经做好了</p>
</blockquote>
<p>java代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Spring boot方式  </span></span><br><span class="line"><span class="meta">@EnableTransactionManagement</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@MapperScan(&quot;com.baomidou.cloud.service.*.mapper*&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MybatisPlusConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 分页插件</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PaginationInterceptor <span class="title">paginationInterceptor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PaginationInterceptor();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="meta">@GetMapping(&quot;/artList&quot;)</span></span><br><span class="line">   <span class="keyword">public</span> Map&lt;String,Object&gt;articleList(<span class="keyword">int</span> page,<span class="keyword">int</span> limit,Article article)&#123;</span><br><span class="line">    Page&lt;Article&gt; pageArt=<span class="keyword">new</span> Page&lt;Article&gt;(page,limit);</span><br><span class="line">    Page&lt;Article&gt; page1 = articleMapper.findAllAndPage(pageArt); <span class="comment">//自定义方法，多表</span></span><br><span class="line"><span class="comment">// QueryWrapper&lt;Article&gt; diseaseQueryWrapperw = new QueryWrapper&lt;Article&gt;(article); </span></span><br><span class="line"><span class="comment">// IPage&lt;Article&gt; page1 = articleService.page(pageArt,diseaseQueryWrapperw); </span></span><br><span class="line">       <span class="comment">//自带的分页查询。只能单表</span></span><br><span class="line"><span class="comment">// List&lt;Article&gt; list = articleService.list();</span></span><br><span class="line">    <span class="keyword">int</span> total = (<span class="keyword">int</span>)page1.getTotal();</span><br><span class="line">    <span class="keyword">return</span> TableMap.ResultJson(<span class="number">0</span>,total,<span class="string">&quot;ok&quot;</span>,page1.getRecords()); </span><br><span class="line">    <span class="comment">//layui table 解析返回格式</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="comment">//mapper接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ArticleMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">Article</span>&gt; </span>&#123;</span><br><span class="line">    <span class="comment">//可以给第二模糊查询的参数</span></span><br><span class="line">    <span class="function">Page&lt;Article&gt; <span class="title">findAllAndPage</span><span class="params">( Page&lt;Article&gt; page)</span></span>;</span><br><span class="line"><span class="comment">//IPage&lt;T&gt; selectPage</span></span><br><span class="line"><span class="comment">//    (IPage&lt;T&gt; page, @Param(Constants.WRAPPER) Wrapper&lt;T&gt; queryWrapper);</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//		   SELECT</span></span><br><span class="line"><span class="comment">//	        id,</span></span><br><span class="line"><span class="comment">//	        name,</span></span><br><span class="line"><span class="comment">//	        age,</span></span><br><span class="line"><span class="comment">//	        email,</span></span><br><span class="line"><span class="comment">//	        gmt_create,</span></span><br><span class="line"><span class="comment">//	        gmt_modified AS gmt_modified,</span></span><br><span class="line"><span class="comment">//	        version,</span></span><br><span class="line"><span class="comment">//	        deleted </span></span><br><span class="line"><span class="comment">//	    FROM</span></span><br><span class="line"><span class="comment">//	        user </span></span><br><span class="line"><span class="comment">//	    WHERE</span></span><br><span class="line"><span class="comment">//	        deleted=0 </span></span><br><span class="line"><span class="comment">//	        AND email LIKE &#x27;%qq%&#x27; LIMIT 0,5</span></span><br><span class="line"><span class="comment">//Page&lt;User&gt; page = new Page&lt;&gt;(1,5);</span></span><br><span class="line"><span class="comment">//QueryWrapper&lt;User&gt; qw=new QueryWrapper&lt;User&gt;();</span></span><br></pre></td></tr></table></figure>

<p>xml代码</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">&quot;findAllAndPage&quot;</span> </span></span><br><span class="line"><span class="tag">      <span class="attr">resultType</span>=<span class="string">&quot;com.chaoba.shirodemo1.model.Article&quot;</span>&gt;</span></span><br><span class="line">    SELECT a.id,a.title,a.uid,a.isDel,a.createTime,a.type,ae.`name` </span><br><span class="line">    FROM article a JOIN article_enum ae ON </span><br><span class="line">    a.type=ae.id</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.baomidou<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.7.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>虽然需要自己写sql，但是减少模糊查询和分页</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 用户表 Mapper 接口</span></span><br><span class="line"><span class="comment"> * &lt;/p&gt;</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function">List&lt;UserListModel&gt; <span class="title">selectUserListPage</span><span class="params">(Pagination page ,</span></span></span><br><span class="line"><span class="params"><span class="function">                                       <span class="meta">@Param(&quot;user&quot;)</span> UserListBean user)</span></span>;   </span><br><span class="line">&#125;</span><br><span class="line">&lt;select id=<span class="string">&quot;selectUserListPage&quot;</span> resultType=<span class="string">&quot;com.web.member.model.UserListModel&quot;</span>&gt;</span><br><span class="line">    SELECT</span><br><span class="line">    *</span><br><span class="line">    FROM</span><br><span class="line">    ftms_user u</span><br><span class="line">    LEFT JOIN ftms_user_level l ON u.level_id = l.id</span><br><span class="line">    WHERE <span class="number">1</span>=<span class="number">1</span></span><br><span class="line">    &lt;<span class="keyword">if</span> test=<span class="string">&quot;user.nickname != null&quot;</span>&gt;</span><br><span class="line">        and u.nickname like <span class="string">&quot;%&quot;</span>#&#123;user.nickname&#125;<span class="string">&quot;%&quot;</span> </span><br><span class="line">     &lt;/<span class="keyword">if</span>&gt;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line">            </span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> </span></span><br><span class="line"><span class="class">    <span class="keyword">extends</span> <span class="title">ServiceImpl</span>&lt;<span class="title">UserMapper</span>, <span class="title">User</span>&gt; </span></span><br><span class="line"><span class="class">    <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Transactional(readOnly=true)</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Page&lt;UserListModel&gt; <span class="title">selectUserListPage</span><span class="params">(UserListBean user)</span> </span>&#123;</span><br><span class="line">        Page&lt;UserListModel&gt; page = </span><br><span class="line">            <span class="keyword">new</span> Page&lt;&gt;(user.getCurr(), user.getNums());</span><br><span class="line">        <span class="comment">// 当前页，总条数 构造 page 对象</span></span><br><span class="line">        <span class="keyword">return</span> page.setRecords(<span class="keyword">this</span>.baseMapper.selectUserListPage(page, user));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这是service层，多表分页的简单逻辑处理，重点是IPage 和 Map，</span></span><br><span class="line">	<span class="comment">// IPage 就是分页，把分页的参数传进来进行使用</span></span><br><span class="line">	<span class="comment">// Map 是用来传参数使用的，携带你需要的参数</span></span><br><span class="line">	IPage&lt;User&gt; iPage = <span class="keyword">new</span> Page&lt;&gt;(pageNum, pageSize);</span><br><span class="line">    Map map=<span class="keyword">new</span> HashMap();</span><br><span class="line">    map.put(<span class="string">&quot;role_id&quot;</span>, roleId);</span><br><span class="line">    IPage&lt;User&gt; usersFromRole = userMapper.getUsersFromRole(iPage, map);</span><br><span class="line"></span><br><span class="line"><span class="comment">// service中传过来的两个参数，注意 map的使用</span></span><br><span class="line"><span class="function">IPage&lt;User&gt; <span class="title">getUsersFromRole</span><span class="params">(IPage&lt;User&gt; page, <span class="meta">@Param(&quot;map&quot;)</span> Map map)</span></span>;</span><br><span class="line"></span><br><span class="line">&lt;--这里同样注意map的使用--!&gt;</span><br><span class="line">&lt;select id=<span class="string">&quot;getUsersFromRole&quot;</span> resultMap=<span class="string">&quot;BaseResultMap&quot;</span>&gt;</span><br><span class="line">    SELECT u.*</span><br><span class="line">    FROM sys_user_role c, sys_user u</span><br><span class="line">    WHERE c.user_id = u.id AND c.role_id = #&#123;map.role_id&#125;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="多表-分页-模糊"><a href="#多表-分页-模糊" class="headerlink" title="多表+分页+模糊"></a>多表+分页+模糊</h3><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><blockquote>
<p>感想：mp解决的单表大部分操作，对于多表来说就不那么友好，但是，好在解决了其它的麻烦。</p>
<p>比如分页和条件查询，它都帮我们解决了  多表的话建议使用内连接配合条件查询</p>
</blockquote>
<h3 id="mp的使用"><a href="#mp的使用" class="headerlink" title="mp的使用"></a>mp的使用</h3><h4 id="1-快速入门"><a href="#1-快速入门" class="headerlink" title="1.快速入门"></a>1.快速入门</h4><p>新建maven过程</p>
<p>导入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.myh<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mp<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2.0.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">relativePath</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- lookup parent from repository --&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- 启动器 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- test --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- lombok --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- MySQL --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- mybatis-plus启动器--&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.baomidou<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.1.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>创建配置文件</p>
<p>application.yml</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">spring:</span><br><span class="line">  datasource:</span><br><span class="line">    driver-class-name: com.mysql.cj.jdbc.Driver</span><br><span class="line">    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8</span><br><span class="line">    username: root</span><br><span class="line">    password: 123456</span><br></pre></td></tr></table></figure>

<p>创建启动类</p>
<p>创建环境目录</p>
<p>==创建test目录  要和环境一样的  否则报错==</p>
<p>2.创建bean对象 编写mapper</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//mapper</span></span><br><span class="line"><span class="meta">@Mapper</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">User</span>&gt;</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//bean</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@TableName(&quot;user2&quot;)</span><span class="comment">//我的数据库表名为user2 </span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> Long id; <span class="comment">//主键</span></span><br><span class="line">	<span class="keyword">private</span> String name; <span class="comment">//姓名</span></span><br><span class="line">	<span class="keyword">private</span> Integer age; <span class="comment">//年龄</span></span><br><span class="line">	<span class="keyword">private</span> String email;<span class="comment">//邮箱</span></span><br><span class="line">	<span class="keyword">private</span> Long managerId;<span class="comment">//直属上级</span></span><br><span class="line">	<span class="keyword">private</span> LocalDateTime createTime;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>3.test</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SimplTest</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Autowired</span></span><br><span class="line">	<span class="keyword">private</span> UserMapper userMapper;</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">select</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		List&lt;User&gt; list = userMapper.selectList(<span class="keyword">null</span>);</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: </span></span><br><span class="line"><span class="comment">//TRACE&lt;==    Columns: id, name, age, email, manager_id, create_time</span></span><br></pre></td></tr></table></figure>

<h4 id="2-传通的模式和mapper的新增方法"><a href="#2-传通的模式和mapper的新增方法" class="headerlink" title="2.传通的模式和mapper的新增方法"></a>2.传通的模式和mapper的新增方法</h4><blockquote>
<p>1.接口中写抽象方法</p>
<p>2.xml或者注解写sql</p>
<p>3.service中调用接口</p>
<p>4.controller调用</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">copy 项目</span><br><span class="line">小技巧：将当前项目copy 修改	artifactId的属性和项目一样即可</span><br><span class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.myh<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mp<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>增加日志输出</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="attr">driver-class-name:</span> <span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line">    <span class="attr">url:</span> <span class="string">jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8</span></span><br><span class="line">    <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line">    <span class="attr">password:</span> <span class="number">123456</span></span><br><span class="line">    </span><br><span class="line"><span class="attr">logging:</span></span><br><span class="line">  <span class="attr">level:</span></span><br><span class="line">    <span class="attr">root:</span> <span class="string">warn</span></span><br><span class="line">    <span class="attr">com.myh.mapper:</span> <span class="string">trace</span></span><br><span class="line">  <span class="attr">pattern:</span></span><br><span class="line">    <span class="attr">console:</span> <span class="string">&#x27;%p%m%n&#x27;</span>  </span><br></pre></td></tr></table></figure>

<p>代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user=<span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">    user.setAge(<span class="number">31</span>);</span><br><span class="line">    user.setManagerId(<span class="number">1088250446457389058L</span>);</span><br><span class="line">    user.setCreateTime(LocalDateTime.now());</span><br><span class="line">    <span class="keyword">int</span> insert = userMapper.insert(user);</span><br><span class="line">    <span class="comment">//		System.out.println(LocalDateTime.now());//2020-06-20T23:53:12.841</span></span><br><span class="line">    <span class="comment">//DEBUG==&gt;  Preparing: INSERT INTO user2 ( id, name, age, manager_id, create_time ) VALUES ( ?, ?, ?, ?, ? ) </span></span><br><span class="line">    <span class="comment">//DEBUG==&gt; Parameters: 1274370381056466945(Long), aaa(String), 31(Integer), 1088250446457389058(Long), 2020-06-20 23:55:49.935(Timestamp)</span></span><br><span class="line">    <span class="comment">//DEBUG&lt;==    Updates: 1s</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>思考：我没有设置email 在插入使也没有email这个列？</p>
<p>所以，在我们插入使，如果实体对象的为null，就不会显示在插入语句中</p>
<p>不信你看！</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user=<span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="keyword">null</span>);</span><br><span class="line">    user.setAge(<span class="number">31</span>);</span><br><span class="line">    user.setManagerId(<span class="number">1088250446457389058L</span>);</span><br><span class="line">    user.setCreateTime(LocalDateTime.now());</span><br><span class="line">    <span class="keyword">int</span> insert = userMapper.insert(user);</span><br><span class="line">    <span class="comment">//DEBUG==&gt;  Preparing: INSERT INTO user2 ( id, age, manager_id, create_time ) VALUES ( ?, ?, ?, ? ) </span></span><br><span class="line">    <span class="comment">//DEBUG==&gt; Parameters: 1274371310874931201(Long), 31(Integer), 1088250446457389058(Long), 2020-06-20 23:59:31.617(Timestamp)</span></span><br><span class="line">    <span class="comment">//DEBUG&lt;==    Updates: 1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//没有name这个列 原因是它为null</span></span><br></pre></td></tr></table></figure>

<h4 id="3-常用注解"><a href="#3-常用注解" class="headerlink" title="3.常用注解"></a>3.常用注解</h4><blockquote>
<p>id 默认基于雪花算法的id</p>
<p>默认使用驼峰法</p>
<p>mp默认去找主键为id的字段 去使用雪花算法赋值  如果你不数据库和字段不叫id应该使用注解    @TableId</p>
<p>mp默认字段和表列对应</p>
<p>mp自动映射全部普通字段  复杂的不算（比如其它类名）</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="meta">@Retention(RetentionPolicy.RUNTIME)</span></span><br><span class="line"><span class="meta">@Target(ElementType.FIELD)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> TableField &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字段值（驼峰命名方式，该值可无）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">String <span class="title">value</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 当该Field为类对象时, 可使用#&#123;对象.属性&#125;来映射到数据表.</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;支持：<span class="doctag">@TableField</span>(el = &quot;role, jdbcType=BIGINT)&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;支持：<span class="doctag">@TableField</span>(el = &quot;role, typeHandler=com.baomidou.springcloud.typehandler.PhoneTypeHandler&quot;)&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">String <span class="title">el</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 是否为数据库表字段</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;默认 true 存在，false 不存在&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">exist</span><span class="params">()</span> <span class="keyword">default</span> <span class="keyword">true</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字段 where 实体查询比较条件</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;默认 `=` 等值&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">String <span class="title">condition</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字段 update set 部分注入, 该注解优于 el 注解使用</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;例如：<span class="doctag">@TableField</span>(.. , update=&quot;%s+1&quot;) 其中 %s 会填充为字段&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;输出 SQL 为：update 表 set 字段=字段+1 where ...&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;例如：<span class="doctag">@TableField</span>(.. , update=&quot;now()&quot;) 使用数据库时间&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;输出 SQL 为：update 表 set 字段=now() where ...&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">String <span class="title">update</span><span class="params">()</span> <span class="keyword">default</span> &quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字段验证策略</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;默认追随全局配置&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">FieldStrategy <span class="title">strategy</span><span class="params">()</span> <span class="keyword">default</span> FieldStrategy.DEFAULT</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 字段自动填充策略</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function">FieldFill <span class="title">fill</span><span class="params">()</span> <span class="keyword">default</span> FieldFill.DEFAULT</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 是否进行 select 查询</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;大字段可设置为 false 不加入 select 查询范围&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">select</span><span class="params">()</span> <span class="keyword">default</span> <span class="keyword">true</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>==@TableName== 指定bean对于的数据库的表名</p>
<p>==@TableId== 主键id   解决id和数据库id名不一致</p>
<p>==@TableField== 解决字段不一致</p>
<h4 id="4-排除非表的字段三种方式"><a href="#4-排除非表的字段三种方式" class="headerlink" title="4.排除非表的字段三种方式"></a>4.排除非表的字段三种方式</h4><ol>
<li>private transient String remark; 表示不是数据库字段 不参与序列化</li>
<li>private static String remark;//参与序列化 也忽略数据库字段</li>
<li>@TableField(exist = false)<br>private String remark; //也忽略数据库字段  推挤使用</li>
</ol>
<h4 id="5-查询"><a href="#5-查询" class="headerlink" title="5 .查询"></a>5 .查询</h4><p>一个</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">select</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = userMapper.selectById(<span class="number">1094590409767661570L</span>);</span><br><span class="line"><span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE id=? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>多个</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByIdS</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectBatchIds(Arrays.asList(<span class="number">1094590409767661570L</span>,<span class="number">1274370381056466945L</span>,<span class="number">1274370381056466945L</span>));</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE id IN ( ? , ? , ? ) </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>selectMap</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 1、名字中包含雨并且年龄小于40</span></span><br><span class="line"><span class="comment">	name like &#x27;%雨%&#x27; and age&lt;40</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectMaps</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">    map.put(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;a&quot;</span>);<span class="comment">//存的是数据库的列</span></span><br><span class="line">    map.put(<span class="string">&quot;age&quot;</span>, <span class="number">23</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectByMap(map);</span><br><span class="line"> <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line"> <span class="comment">//FROM user2 WHERE name = ? AND age = ? 		</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="6-条件构造器"><a href="#6-条件构造器" class="headerlink" title="6.条件构造器"></a>6.条件构造器</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();</span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;雨&quot;</span>).lt(<span class="string">&quot;age&quot;</span>, <span class="number">23</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE name LIKE ? AND age &lt; ? 		</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 	名字中包含雨年并且龄大于等于20且小于等于40并且email不为空</span></span><br><span class="line"><span class="comment"> *    name like &#x27;%雨%&#x27; and age between 20 and 40 and email is not null</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();</span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;雨&quot;</span>).between(<span class="string">&quot;age&quot;</span>, <span class="number">2</span>, <span class="number">20</span>).isNotNull(<span class="string">&quot;email&quot;</span>);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2</span></span><br><span class="line">    <span class="comment">//WHERE name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL 		</span></span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 3、名字为王姓或者年龄大于等于25，按照年龄降序排列，年龄相同按照id升序排列</span></span><br><span class="line"><span class="comment">   name like &#x27;王%&#x27; or age&gt;=25 order by age desc,id asc</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();</span><br><span class="line">    queryWrapper.likeRight(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>)</span><br><span class="line">        .or().ge(<span class="string">&quot;age&quot;</span>,<span class="number">23</span>).orderByDesc(<span class="string">&quot;age&quot;</span>).orderByAsc(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE name LIKE ? OR age &gt;= ? ORDER BY age DESC , id ASC </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 4、创建日期为2019年2月14日并且直属上级为名字为王姓</span></span><br><span class="line"><span class="comment">      date_format(create_time,&#x27;%Y-%m-%d&#x27;)=&#x27;2019-02-14&#x27; </span></span><br><span class="line"><span class="comment">      and manager_id in (select id from user where name like &#x27;王%&#x27;)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.apply(<span class="string">&quot;date_format(create_time,&#x27;%Y-%m-%d&#x27;)=&#123;0&#125;&quot;</span>, <span class="string">&quot;2019-02-14&quot;</span>)</span><br><span class="line">        .inSql(<span class="string">&quot;manager_id&quot;</span>, <span class="string">&quot;select id from user2 where name like &#x27;%王&#x27;&quot;</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE date_format(create_time,&#x27;%Y-%m-%d&#x27;)=? AND </span></span><br><span class="line">    <span class="comment">//manager_id IN (select id from user2 where name like &#x27;%王&#x27;) </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 5、名字为王姓并且（年龄小于40或邮箱不为空）</span></span><br><span class="line"><span class="comment">    name like &#x27;王%&#x27; and (age&lt;40 or email is not null)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper5</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.likeRight(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>)</span><br><span class="line">        .and(wq-&gt;wq.lt(<span class="string">&quot;age&quot;</span>,<span class="number">10</span>).or().isNotNull(<span class="string">&quot;email&quot;</span>));</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE name LIKE ? AND ( age &lt; ? OR email IS NOT NULL ) </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 6、名字为王姓或者（年龄小于40并且年龄大于20并且邮箱不为空）</span></span><br><span class="line"><span class="comment">    name like &#x27;王%&#x27; or (age&lt;40 and age&gt;20 and email is not null)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper6</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.likeRight(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>)</span><br><span class="line">        .or(wq-&gt;wq.lt(<span class="string">&quot;age&quot;</span>,<span class="number">10</span>).gt(<span class="string">&quot;age&quot;</span>, <span class="number">20</span>).isNotNull(<span class="string">&quot;email&quot;</span>));</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE name LIKE ? OR ( age &lt; ? AND age &gt; ? AND email IS NOT NULL ) </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 7、（年龄小于40或邮箱不为空）并且名字为王姓</span></span><br><span class="line"><span class="comment">    (age&lt;40 or email is not null) and name like &#x27;王%&#x27;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">	</span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper7</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.nested(wq-&gt;wq.lt(<span class="string">&quot;age&quot;</span>, <span class="number">60</span>).or().isNotNull(<span class="string">&quot;email&quot;</span>))</span><br><span class="line">        .likeRight(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//FROM user2 WHERE ( age &lt; ? OR email IS NOT NULL ) AND name LIKE ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 8、年龄为30、31、34、35</span></span><br><span class="line"><span class="comment">    age in (30、31、34、35)  </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper8</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line"><span class="comment">//		queryWrapper.in(&quot;age&quot;,12,12,23);</span></span><br><span class="line">		queryWrapper.in(<span class="string">&quot;age&quot;</span>,Arrays.asList(<span class="number">21</span>,<span class="number">21</span>,<span class="number">21</span>));  </span><br><span class="line">		List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line"><span class="comment">//		SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line"><span class="comment">//		FROM user2 WHERE age IN (?,?,?) </span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>拼接SQL</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 9、只返回满足条件的其中一条语句即可</span></span><br><span class="line"><span class="comment">	limit 1</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapper9</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    <span class="comment">//		queryWrapper.in(&quot;age&quot;,12,12,23);</span></span><br><span class="line">    <span class="comment">//sql注入</span></span><br><span class="line">    queryWrapper.in(<span class="string">&quot;age&quot;</span>,Arrays.asList(<span class="number">21</span>,<span class="number">21</span>,<span class="number">21</span>)).last(<span class="string">&quot;limit 1&quot;</span>);  </span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//		SELECT id,name,age,email,manager_id,create_time </span></span><br><span class="line">    <span class="comment">//		FROM user2 WHERE age IN (?,?,?) limit 1 </span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="7-查询指定字段"><a href="#7-查询指定字段" class="headerlink" title="7.查询指定字段"></a>7.查询指定字段</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 10、名字中包含雨并且年龄小于40(需求1加强版)</span></span><br><span class="line"><span class="comment">        第一种情况：select id,name</span></span><br><span class="line"><span class="comment">   from user</span></span><br><span class="line"><span class="comment">   where name like &#x27;%雨%&#x27; and age&lt;40</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperSupper</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.in(<span class="string">&quot;age&quot;</span>,Arrays.asList(<span class="number">21</span>,<span class="number">21</span>,<span class="number">21</span>)).last(<span class="string">&quot;limit 1&quot;</span>).select(<span class="string">&quot;id,name&quot;</span>);  </span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name FROM user2 WHERE age IN (?,?,?) limit 1 </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//默认查出的字段为空</span></span><br></pre></td></tr></table></figure>

<p>排除指定字段</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//	第二种情况：select id,name,age,email</span></span><br><span class="line"><span class="comment">//    from user</span></span><br><span class="line"><span class="comment">//    where name like &#x27;%雨%&#x27; and age&lt;40</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperSupper2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.likeRight(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>)</span><br><span class="line">        .select(User.class,info-&gt;!info.getColumn().equals(<span class="string">&quot;age&quot;</span>)&amp;&amp;!info.getColumn().equals(<span class="string">&quot;name&quot;</span>));</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,email,manager_id,create_time FROM user2 WHERE name LIKE ?  </span></span><br><span class="line">    <span class="comment">//测试  id不能排除</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>####8. condition 执行条件 </p>
<p>为true就加入  false不加入</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrappercondition</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    String name=<span class="string">&quot;12&quot;</span>;</span><br><span class="line">    String email=<span class="string">&quot;&quot;</span>;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.like(!StringUtils.isEmpty(name),<span class="string">&quot;name&quot;</span>, name)</span><br><span class="line">        .like(!StringUtils.isEmpty(email),<span class="string">&quot;name&quot;</span>, email);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name LIKE ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="9-实体对象作为构造器的参数"><a href="#9-实体对象作为构造器的参数" class="headerlink" title="9.实体对象作为构造器的参数"></a>9.实体对象作为构造器的参数</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#125;</span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperEntily</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    User user=<span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">    user.setAge(<span class="number">12</span>);</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;(user);<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name=? AND age=? </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//默认的等值的</span></span><br><span class="line"><span class="comment">//	@TableField(condition =SqlCondition.EQUAL )</span></span><br><span class="line"><span class="comment">//	private String name; //姓名</span></span><br><span class="line"><span class="comment">//可以手动修改</span></span><br></pre></td></tr></table></figure>

<p>不冲突慎用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperEntity2</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    User user=<span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">    user.setAge(<span class="number">12</span>);</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;(user);<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;a&quot;</span>);</span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name=? AND age=? AND name LIKE ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="10-allEq用户"><a href="#10-allEq用户" class="headerlink" title="10.allEq用户"></a>10.allEq用户</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * map 所有非空属性等于 =</span></span><br><span class="line"><span class="comment">	 *</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> condition   执行条件</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> params      map 类型的参数, key 是字段名, value 是字段值</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@param</span> null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段\</span></span><br><span class="line"><span class="comment">	 * <span class="doctag">@return</span> children</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperAllEq</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">    map.put(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>);</span><br><span class="line">    map.put(<span class="string">&quot;age&quot;</span>, <span class="keyword">null</span>);</span><br><span class="line">    queryWrapper.allEq(map,<span class="keyword">false</span>);<span class="comment">//忽略为空的字段</span></span><br><span class="line">    List&lt;User&gt; list = userMapper.selectList(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//		SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name = ? AND age = ? </span></span><br><span class="line">    <span class="comment">//		SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name = ? AND age IS NULL  map.put(&quot;age&quot;, null);</span></span><br><span class="line">    <span class="comment">//		 Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name = ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>####11.其它条件构造器为为参数</p>
<p>selectByWrapperMaps</p>
<p>应用场景：如果返回实体对象的list，可能很多字段都是null，但是使用这个就不会出现null</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperMaps</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>);</span><br><span class="line">    List&lt;Map&lt;String,Object&gt;&gt; list = userMapper.selectMaps(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE name LIKE ? </span></span><br><span class="line">&#123;create_time=<span class="number">2019</span>-<span class="number">01</span>-<span class="number">11</span> <span class="number">22</span>:<span class="number">20</span>:<span class="number">20.0</span>, name=大boss, id=<span class="number">1087982257332887553</span>, age=<span class="number">40</span>, email=boss<span class="meta">@baomidou</span>.com&#125;</span><br><span class="line">&#123;create_time=<span class="number">2019</span>-<span class="number">02</span>-<span class="number">05</span> <span class="number">19</span>:<span class="number">12</span>:<span class="number">22.0</span>, manager_id=<span class="number">1087982257332887553</span>, name=王天风, id=<span class="number">1088248166370832385</span>, age=<span class="number">25</span>, email=wtf<span class="meta">@baomidou</span>.com&#125;</span><br><span class="line">&#123;create_time=<span class="number">2019</span>-<span class="number">02</span>-<span class="number">14</span> <span class="number">16</span>:<span class="number">31</span>:<span class="number">16.0</span>, manager_id=<span class="number">1088248166370832385</span>, name=李艺伟, id=<span class="number">1088250446457389058</span>, age=<span class="number">28</span>, email=lyw<span class="meta">@baomidou</span>.com&#125;</span><br><span class="line">&#123;create_time=<span class="number">2019</span>-<span class="number">01</span>-<span class="number">14</span> <span class="number">17</span>:<span class="number">15</span>:<span class="number">15.0</span>, manager_id=<span class="number">1088248166370832385</span>, name=张雨琪, id=<span class="number">1094590409767661570</span>, age=<span class="number">31</span>, email=zjq<span class="meta">@baomidou</span>.com&#125;</span><br><span class="line">&#123;create_time=<span class="number">2019</span>-<span class="number">01</span>-<span class="number">14</span> <span class="number">17</span>:<span class="number">48</span>:<span class="number">16.0</span>, manager_id=<span class="number">1088248166370832385</span>, name=刘红雨, id=<span class="number">1094592041087729666</span>, age=<span class="number">32</span>, email=lhm<span class="meta">@baomidou</span>.com&#125;</span><br><span class="line">&#123;create_time=<span class="number">2020</span>-<span class="number">06</span>-<span class="number">20</span> <span class="number">23</span>:<span class="number">55</span>:<span class="number">50.0</span>, manager_id=<span class="number">1088250446457389058</span>, name=aaa, id=<span class="number">1274370381056466945</span>, age=<span class="number">31</span>&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperMaps</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">	QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">	queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;&quot;</span>).select(<span class="string">&quot;id&quot;</span>,<span class="string">&quot;name&quot;</span>);</span><br><span class="line">	List&lt;Map&lt;String,Object&gt;&gt; list = userMapper.selectMaps(queryWrapper);</span><br><span class="line">	list.forEach(System.out::println);</span><br><span class="line">&#125;</span><br><span class="line">&#123;name=大boss, id=<span class="number">1087982257332887553</span>&#125;</span><br><span class="line">&#123;name=王天风, id=<span class="number">1088248166370832385</span>&#125;</span><br><span class="line">&#123;name=李艺伟, id=<span class="number">1088250446457389058</span>&#125;</span><br><span class="line">&#123;name=张雨琪, id=<span class="number">1094590409767661570</span>&#125;</span><br><span class="line">&#123;name=刘红雨, id=<span class="number">1094592041087729666</span>&#125;</span><br><span class="line">&#123;name=aaa, id=<span class="number">1274370381056466945</span>&#125;</span><br></pre></td></tr></table></figure>
<p>如果返回的数据  在实体类中没有  可以使用此方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 9、按照直属上级分组，查询每组的平均年龄、最大年龄、最小年龄。</span></span><br><span class="line"><span class="comment">	并且只取年龄总和小于500的组。</span></span><br><span class="line"><span class="comment">	select avg(age) avg_age,min(age) min_age,max(age) max_age</span></span><br><span class="line"><span class="comment">	from user</span></span><br><span class="line"><span class="comment">	group by manager_id</span></span><br><span class="line"><span class="comment">	having sum(age) &lt;500</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperMaps2</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">		QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">		queryWrapper.select(<span class="string">&quot;avg(age) avg_age&quot;</span>,<span class="string">&quot;min(age) min_age&quot;</span>,<span class="string">&quot;max(age) max_age&quot;</span>)</span><br><span class="line">			.groupBy(<span class="string">&quot;manager_id&quot;</span>).having(<span class="string">&quot;sum(age)&lt;&#123;0&#125;&quot;</span>, <span class="number">500</span>);<span class="comment">//防止sql注入</span></span><br><span class="line">		List&lt;Map&lt;String,Object&gt;&gt; list = userMapper.selectMaps(queryWrapper);</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line"><span class="comment">//		SELECT avg(age) avg_age,min(age) min_age,max(age) max_age FROM user2 GROUP BY manager_id HAVING sum(age)&lt;? </span></span><br><span class="line">	&#125;</span><br><span class="line">&#123;max_age=<span class="number">40</span>, avg_age=<span class="number">40.0000</span>, min_age=<span class="number">40</span>&#125;</span><br><span class="line">&#123;max_age=<span class="number">25</span>, avg_age=<span class="number">25.0000</span>, min_age=<span class="number">25</span>&#125;</span><br><span class="line">&#123;max_age=<span class="number">32</span>, avg_age=<span class="number">30.3333</span>, min_age=<span class="number">28</span>&#125;</span><br><span class="line">&#123;max_age=<span class="number">31</span>, avg_age=<span class="number">31.0000</span>, min_age=<span class="number">31</span>&#125;	</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//只返回第一列 值每一列的值  不知道是什么 所以为object</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperObjs</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;&quot;</span>).select(<span class="string">&quot;id&quot;</span>,<span class="string">&quot;name&quot;</span>);</span><br><span class="line">    List&lt;Object&gt; list = userMapper.selectObjs(queryWrapper);</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>查记录数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperCount</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">		QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">		</span><br><span class="line">		HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">		map.put(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;王&quot;</span>);</span><br><span class="line">		map.put(<span class="string">&quot;age&quot;</span>, <span class="keyword">null</span>);<span class="comment">//默认为= 可设置</span></span><br><span class="line">		queryWrapper.allEq(map,<span class="keyword">false</span>);<span class="comment">//忽略为空的字段</span></span><br><span class="line"><span class="comment">//		SELECT COUNT( 1 ) FROM user2 WHERE name = ? </span></span><br><span class="line">		</span><br><span class="line"><span class="comment">//		queryWrapper.like(&quot;name&quot;, &quot;&quot;).lt(&quot;age&quot;, 12);</span></span><br><span class="line"><span class="comment">//		SELECT COUNT( 1 ) FROM user2 WHERE name LIKE ? AND age &lt; ? </span></span><br><span class="line">		Integer selectCount = userMapper.selectCount(queryWrapper);</span><br><span class="line">		System.out.println(selectCount);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>selectOne</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByWrapperOne</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.like(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">    User user = userMapper.selectOne(queryWrapper);  <span class="comment">//没有就为null 只能一个 多个报错</span></span><br><span class="line">    System.out.println(user);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>####12.自定义SQL </p>
<blockquote>
<p>版本大于3.0.7</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Mapper</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">User</span>&gt;</span>&#123;</span><br><span class="line">	<span class="comment">//sql也可以写在xml文件中</span></span><br><span class="line">	<span class="meta">@Select(&quot;select * from user2 $&#123;ew.customSqlSegment&#125;&quot;)</span></span><br><span class="line">	<span class="function">List&lt;User&gt; <span class="title">selectAll</span><span class="params">(<span class="meta">@Param(Constants.WRAPPER)</span> Wrapper&lt;User&gt; wrapper)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 自定义方法</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectByMe</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">		QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">		queryWrapper.like(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;aa&quot;</span>);</span><br><span class="line">		List&lt;User&gt; list = userMapper.selectAll(queryWrapper);</span><br><span class="line">		list.forEach(System.out::println);</span><br><span class="line"><span class="comment">//		select * from user2 WHERE name LIKE ? </span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>配置·</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line">  <span class="attr">mapper-locations:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">mybatis/mapper/*Mapper.xml</span></span><br><span class="line"><span class="string">&lt;!--和mybatis一样--&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="13-分页查询"><a href="#13-分页查询" class="headerlink" title="13.分页查询"></a>13.分页查询</h4><p>mp实现了物理分页 就是limit</p>
<p>mybatis有RoundS  这是逻辑分页  一次查完 然后筛选</p>
<ol>
<li>注册插件</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span> <span class="comment">//配置类</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MybatisPlusConfig</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Bean</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> PaginationInterceptor <span class="title">paginationInterceptor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> PaginationInterceptor();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>测试·</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectPage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.ge(<span class="string">&quot;age&quot;</span>, <span class="number">12</span>);</span><br><span class="line">    Page&lt;User&gt; page = <span class="keyword">new</span> Page&lt;User&gt;(<span class="number">1</span>,<span class="number">2</span>,<span class="keyword">false</span>);</span><br><span class="line">    <span class="comment">//false   不要总记录数 默认为true</span></span><br><span class="line">    <span class="comment">//SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE age &gt;= ? LIMIT ?,?  </span></span><br><span class="line">    IPage&lt;User&gt; selectPage = userMapper.selectPage(page, queryWrapper);</span><br><span class="line">    <span class="keyword">long</span> total = selectPage.getTotal();<span class="comment">//设置为false 拿不到总记录数</span></span><br><span class="line">	System.out.println(total);</span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: SELECT COUNT(1) FROM user2 WHERE age &gt;= ? </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: 12(Integer)</span></span><br><span class="line"><span class="comment">//TRACE&lt;==    Columns: COUNT(1)</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 7</span></span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE age &gt;= ? LIMIT ?,? </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: 12(Integer), 0(Long), 2(Long)</span></span><br><span class="line"><span class="comment">//TRACE&lt;==    Columns: id, name, age, email, manager_id, create_time</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22</span></span><br><span class="line"><span class="comment">//DEBUG&lt;==      Total: 2		</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectPage2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.ge(<span class="string">&quot;age&quot;</span>, <span class="number">12</span>);</span><br><span class="line">    Page&lt;User&gt; page = <span class="keyword">new</span> Page&lt;User&gt;(<span class="number">1</span>,<span class="number">2</span>,<span class="keyword">false</span>);</span><br><span class="line">    IPage&lt;Map&lt;String,Object&gt;&gt; selectMapsPage = userMapper.selectMapsPage(page, queryWrapper);</span><br><span class="line">    List&lt;Map&lt;String,Object&gt;&gt; list = selectMapsPage.getRecords();</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line">    <span class="comment">//泛型不一样</span></span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE age &gt;= ? LIMIT ?,? </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: 12(Integer), 0(Long), 2(Long)</span></span><br><span class="line"><span class="comment">//TRACE&lt;==    Columns: id, name, age, email, manager_id, create_time</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22</span></span><br><span class="line"><span class="comment">//DEBUG&lt;==      Total: 2</span></span><br><span class="line"><span class="comment">//&#123;create_time=2019-01-11 22:20:20.0, name=大boss, id=1087982257332887553, age=40, email=boss@baomidou.com&#125;</span></span><br><span class="line"><span class="comment">//&#123;create_time=2019-02-05 19:12:22.0, manager_id=1087982257332887553, name=王天风, id=1088248166370832385, age=25, email=wtf@baomidou.com&#125;</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h4 id="14-自定义分页sql"><a href="#14-自定义分页sql" class="headerlink" title="14 自定义分页sql"></a>14 自定义分页sql</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">IPage&lt;User&gt; <span class="title">selectUserPage</span><span class="params">(Page&lt;User&gt; page,<span class="meta">@Param(Constants.WRAPPER)</span> Wrapper&lt;User&gt; wrapper)</span></span>;</span><br><span class="line"></span><br><span class="line">&lt;select id=<span class="string">&quot;selectUserPage&quot;</span>  resultType=<span class="string">&quot;com.myh.pojo.User&quot;</span>&gt;</span><br><span class="line">	select * from user2 $&#123;ew.customSqlSegment&#125;</span><br><span class="line">&lt;/select&gt;</span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">selectPageMe</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; queryWrapper = <span class="keyword">new</span> QueryWrapper&lt;User&gt;();<span class="comment">//&#123;0&#125;表示第一个参数，复制SQL注入</span></span><br><span class="line">    queryWrapper.ge(<span class="string">&quot;age&quot;</span>, <span class="number">12</span>);</span><br><span class="line">    Page&lt;User&gt; page = <span class="keyword">new</span> Page&lt;User&gt;(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    IPage&lt;User&gt; selectUserPage = userMapper.selectUserPage(page, queryWrapper);</span><br><span class="line">    List&lt;User&gt; list = selectUserPage.getRecords();</span><br><span class="line">    list.forEach(System.out::println);</span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: select * from user2 WHERE age &gt;= ? LIMIT ?,? </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: 12(Integer), 0(Long), 2(Long)</span></span><br><span class="line"><span class="comment">//TRACE&lt;==    Columns: id, name, age, email, manager_id, create_time</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20</span></span><br><span class="line"><span class="comment">//TRACE&lt;==        Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22</span></span><br><span class="line"><span class="comment">//DEBUG&lt;==      Total: 2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//如果使用的是多表 建议使用inne 内连接 不能用where</span></span><br></pre></td></tr></table></figure>

<h4 id="14-更新和删除"><a href="#14-更新和删除" class="headerlink" title="14.更新和删除"></a>14.更新和删除</h4><p>根据==id==修改</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateById</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;AAA&quot;</span>);</span><br><span class="line">    user.setId(<span class="number">1274370381056466945L</span>);</span><br><span class="line">    <span class="keyword">int</span> updateById = userMapper.updateById(user);</span><br><span class="line">    <span class="comment">//UPDATE user2 SET name=? WHERE id=? </span></span><br><span class="line">    <span class="comment">//Parameters: AAA(String), 1274370381056466945(Long)</span></span><br><span class="line">    <span class="comment">//Updates: 1	</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>自定义条件修改==UpdateWrapper==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateByWapper</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    UpdateWrapper&lt;User&gt; uw = <span class="keyword">new</span> UpdateWrapper&lt;User&gt;();</span><br><span class="line">    uw.eq(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;AAA&quot;</span>).eq(<span class="string">&quot;age&quot;</span>,<span class="number">31</span>);</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setAge(<span class="number">13</span>);</span><br><span class="line">    <span class="comment">//实体中为null不会算上去和insert一样</span></span><br><span class="line">    user.setName(<span class="keyword">null</span>);</span><br><span class="line">    <span class="keyword">int</span> updateById = userMapper.update(user, uw);</span><br><span class="line">    <span class="comment">//		UPDATE user2 SET age=? WHERE name = ? AND age = ? </span></span><br><span class="line">    <span class="comment">//		13(Integer), AAA(String), 31(Integer)</span></span><br><span class="line">    <span class="comment">//		Updates: 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>==传入实体==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">	<span class="comment">//传入实体  的时候  可以自定义条件   默认为=   还可以为模糊或者其他大于小于</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateByWapper2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setAge(<span class="number">13</span>);</span><br><span class="line">    user.setName(<span class="keyword">null</span>);</span><br><span class="line">    UpdateWrapper&lt;User&gt; uw = <span class="keyword">new</span> UpdateWrapper&lt;User&gt;(user);</span><br><span class="line">    <span class="comment">//		user.setAge(13);</span></span><br><span class="line">    <span class="comment">//		user.setName(null);sql中会重复出现  </span></span><br><span class="line">    <span class="keyword">int</span> updateById = userMapper.update(user, uw);</span><br><span class="line">    <span class="comment">//		UPDATE user2 SET age=? WHERE age=? AND name = ? AND age = ? </span></span><br><span class="line">    <span class="comment">//		13(Integer), 13(Integer), AAA(String), 31(Integer)</span></span><br><span class="line">    <span class="comment">//		Updates: 0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>修改单个或==少量属性==</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateByWapper3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    UpdateWrapper&lt;User&gt; uw = <span class="keyword">new</span> UpdateWrapper&lt;User&gt;();</span><br><span class="line">    uw.eq(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;AAA&quot;</span>).set(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;aaa&quot;</span>); <span class="comment">//sql的列名</span></span><br><span class="line">    <span class="comment">//修改少量属性</span></span><br><span class="line">    <span class="keyword">int</span> updateById = userMapper.update(<span class="keyword">null</span>, uw);</span><br><span class="line">    Preparing: UPDATE user2 SET name=? WHERE name = ? </span><br><span class="line">        Parameters: aaa(String), AAA(String)</span><br><span class="line">            Updates: <span class="number">1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>根据id删除</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteById</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> id = userMapper.deleteById(<span class="number">1274371310874931201L</span>);</span><br><span class="line">    System.out.println(id);</span><br><span class="line">    <span class="comment">//		DELETE FROM user2 WHERE id=? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>map</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteMap</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    HashMap&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">    map.put(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">    map.put(<span class="string">&quot;age&quot;</span>, <span class="number">12</span>);</span><br><span class="line">    <span class="keyword">int</span> map2 = userMapper.deleteByMap(map);</span><br><span class="line">    <span class="comment">//DELETE FROM user2 WHERE name = ? AND age = ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>删除多个</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteBychIds</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> map2 = userMapper.deleteBatchIds(Arrays.asList(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>));</span><br><span class="line">    <span class="comment">//		DELETE FROM user2 WHERE id IN ( ? , ? , ? ) </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>条件构造器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteWrappers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    QueryWrapper&lt;User&gt; wrapper=<span class="keyword">new</span> QueryWrapper&lt;&gt;();</span><br><span class="line">    wrapper.eq(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;Aaa&quot;</span>);</span><br><span class="line">    userMapper.delete(wrapper);</span><br><span class="line">    <span class="comment">//		DELETE FROM user2 WHERE name = ? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="15-AR模式"><a href="#15-AR模式" class="headerlink" title="15.AR模式"></a>15.AR模式</h4><p>1.对象继承一个Model的类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@TableName(&quot;user2&quot;)</span></span><br><span class="line"><span class="meta">@EqualsAndHashCode(callSuper = false)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span>&lt;<span class="title">User</span>&gt;</span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</span><br><span class="line">	<span class="meta">@TableId</span></span><br><span class="line">	<span class="keyword">private</span> Long id; <span class="comment">//主键</span></span><br><span class="line">	<span class="meta">@TableField(condition =SqlCondition.EQUAL )</span></span><br><span class="line">	<span class="keyword">private</span> String name; <span class="comment">//姓名</span></span><br><span class="line">	<span class="keyword">private</span> Integer age; <span class="comment">//年龄</span></span><br><span class="line">	<span class="keyword">private</span> String email;<span class="comment">//邮箱</span></span><br><span class="line">	<span class="keyword">private</span> Long managerId;<span class="comment">//直属上级</span></span><br><span class="line">	<span class="keyword">private</span> LocalDateTime createTime;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@TableField(exist = false)</span></span><br><span class="line">	<span class="keyword">private</span> String remark;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>2.接口需要继承原始的默认basemapper</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> <span class="keyword">extends</span> <span class="title">BaseMapper</span>&lt;<span class="title">User</span>&gt;</span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>3.测试</p>
<p>insert</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;BBB&quot;</span>);</span><br><span class="line">    <span class="keyword">boolean</span> insert = user.insert();</span><br><span class="line">    System.out.println(insert);</span><br><span class="line">    <span class="comment">//		DEBUG==&gt;  Preparing: INSERT INTO user2 ( id, name ) VALUES ( ?, ? ) </span></span><br><span class="line">    <span class="comment">//		DEBUG==&gt; Parameters: 1274699341518757890(Long), BBB(String)</span></span><br><span class="line">    <span class="comment">//		DEBUG&lt;==    Updates: 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>select</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">select</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		User user = <span class="keyword">new</span> User();</span><br><span class="line"><span class="comment">//		user.setId(1274699799087964161L);</span></span><br><span class="line"><span class="comment">//		User user2 = user.selectById();</span></span><br><span class="line">		User user2 = user.selectById(<span class="number">1274699799087964161L</span>);<span class="comment">//新对象</span></span><br><span class="line">		<span class="comment">// SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE id=? </span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>update</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateById</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setId(<span class="number">1274699799087964161L</span>);</span><br><span class="line">    user.setEmail(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">    <span class="keyword">boolean</span> updateById = user.updateById();</span><br><span class="line">    System.out.println(updateById);</span><br><span class="line">&#125;</span><br><span class="line">                        <span class="number">3.1</span><span class="number">.0</span> </span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: UPDATE user2 SET email=? WHERE id=? </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: aaa(String), 1274699799087964161(Long)</span></span><br><span class="line"><span class="comment">//DEBUG&lt;==    Updates: 1</span></span><br></pre></td></tr></table></figure>

<p>detele</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteById</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setId(<span class="number">1274699799087964161L</span>);</span><br><span class="line">    <span class="keyword">boolean</span> updateById = user.deleteById();</span><br><span class="line">    <span class="comment">//		DELETE FROM user2 WHERE id=? </span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>insertOrUpldate</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insertOrupdate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;CCC&quot;</span>);</span><br><span class="line">    user.setId(<span class="number">1L</span>);</span><br><span class="line">    <span class="comment">//自动判断  如果不设置id 就是insert语句</span></span><br><span class="line">    <span class="comment">//如果设置id 先去查询id查看是否存在  id存在  则是更新语句  如果id 不存在 就是insert语句  并且id为你设置的值</span></span><br><span class="line">    <span class="keyword">boolean</span> update = user.insertOrUpdate();</span><br><span class="line">    System.out.println(update);</span><br><span class="line">    <span class="comment">//		DEBUG==&gt;  Preparing: SELECT id,name,age,email,manager_id,create_time FROM user2 WHERE id=? </span></span><br><span class="line">    <span class="comment">//		DEBUG==&gt; Parameters: 1(Long)</span></span><br><span class="line">    <span class="comment">//		DEBUG&lt;==      Total: 0</span></span><br><span class="line">    <span class="comment">//		DEBUG==&gt;  Preparing: INSERT INTO user2 ( id, name ) VALUES ( ?, ? ) </span></span><br><span class="line">    <span class="comment">//		DEBUG==&gt; Parameters: 1(Long), CCC(String)</span></span><br><span class="line">    <span class="comment">//		DEBUG&lt;==    Updates: 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="16">
<li><p>主键策略</p>
<p>mp支持的主键策略</p>
</li>
</ol>
<p>局部主键策略实现</p>
<p>​    设置主键自增，需要在bean加注解设置自增，在数据库也要设置自增 不然报错</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//自增  注解回显</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		User user = <span class="keyword">new</span> User();</span><br><span class="line">		user.setName(<span class="string">&quot;CCC&quot;</span>);</span><br><span class="line">		<span class="keyword">boolean</span> insert = user.insert();</span><br><span class="line">		System.out.println(user.getId());</span><br><span class="line">   	&#125;</span><br><span class="line"><span class="comment">//DEBUG==&gt;  Preparing: INSERT INTO user2 ( name ) VALUES ( ? ) </span></span><br><span class="line"><span class="comment">//DEBUG==&gt; Parameters: CCC(String)</span></span><br><span class="line"><span class="comment">//DEBUG&lt;==    Updates: 1</span></span><br><span class="line"><span class="comment">//1274699341518757892</span></span><br><span class="line"><span class="comment">//不设置就是默认学号算法</span></span><br><span class="line"><span class="comment">//UUID 是string的</span></span><br><span class="line"><span class="comment">//基于雪花算法还有字符串的</span></span><br><span class="line"><span class="comment">//不主动设置id 就是雪花算法</span></span><br><span class="line"><span class="comment">//上面都是局部</span></span><br><span class="line"><span class="comment">//如果数据库设置id自增  其他设置都不能比数据库的设置优先级大</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>全局主键策略实现</p>
<h4 id="16-需要配置"><a href="#16-需要配置" class="headerlink" title="16.需要配置"></a>16.需要配置</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">mybatis-plus:</span><br><span class="line">  mapper-locations:</span><br><span class="line">  - mybatis/mapper<span class="comment">/*Mapper.xml</span></span><br><span class="line"><span class="comment">  global-config:</span></span><br><span class="line"><span class="comment">    db-config:</span></span><br><span class="line"><span class="comment">      id-type: id-worker</span></span><br><span class="line"><span class="comment">//全局配置</span></span><br><span class="line"><span class="comment">//局部的优先级大于全局</span></span><br><span class="line"><span class="comment">//数据库&gt;局部&gt;全局</span></span><br></pre></td></tr></table></figure>

<p>基本配置</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line">  <span class="attr">mapper-locations:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">mybatis/mapper/*Mapper.xml</span> <span class="comment"># mapper的位置</span></span><br><span class="line">  <span class="attr">global-config:</span></span><br><span class="line">    <span class="attr">db-config:</span></span><br><span class="line">      <span class="attr">id-type:</span> <span class="string">id-worker</span>  <span class="comment"># 全局id配置</span></span><br><span class="line">      <span class="attr">field-strategy:</span> <span class="string">NOT_EMPTY</span> <span class="comment"># 插入时  没有指定字段 默认为null</span></span><br><span class="line">      <span class="attr">tablePrefix:</span> <span class="string">pm_</span> <span class="comment"># 表前缀配置</span></span><br><span class="line">  <span class="attr">config-location:</span> <span class="string">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line">  <span class="attr">type-aliases-package:</span> <span class="string">com.myh.pojo</span> <span class="comment"># </span></span><br><span class="line">  <span class="comment"># configuration:</span></span><br><span class="line">    <span class="comment"># map-underscore-to-camel-case: true #默认开启驼峰</span></span><br><span class="line"> 	<span class="comment"># 绑定mybatis-</span></span><br><span class="line"> 	<span class="comment"># 多模块注意</span></span><br><span class="line"><span class="string">/*********************************************/</span></span><br><span class="line"> <span class="attr">mybatis-plus:</span></span><br><span class="line">  <span class="attr">mapper-locations:</span> <span class="string">classpath*:mybatis/mapper/*Mapper.xml</span></span><br></pre></td></tr></table></figure>

<p>进阶配置 默认为true  在mybatis为false</p>
<p>==map-underscore-to-camel-case: true #默认开启驼峰==</p>
<p> Property ‘configuration’ and ‘configLocation’ can not specified with together不能同时出现</p>
<p>全局配置  但我们insert 时 没有指定的字段为null  默认不会插入 我们可以设置全局配置 让他插入的数据为null</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">INSERT INTO user2 </span><br><span class="line">( id, name, age, email, manager_id, create_time ) </span><br><span class="line">VALUES ( ?, ?, ?, ?, ?, ? )  此时的insert的插入语句</span><br></pre></td></tr></table></figure>

<p>ignored 不好  原因是在uplate使 会将我们没有设置值得字段也会修改为null</p>
<p>not-empty 为null或者空字符串串的  会忽略 </p>
<blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">enum</span> <span class="title">FieldStrategy</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 忽略判断</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    IGNORED,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 非NULL判断</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    NOT_NULL,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 非空判断 </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    NOT_EMPTY,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 默认的,一般只用于注解里</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;1. 在全局里代表 NOT_NULL&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;2. 在注解里代表 跟随全局&lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    DEFAULT</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<p>如果设置了局部策略 那么全局策略就会被覆盖 局部策略由于全局策略</p>
<p> tablePrefix: pm_ # 表前缀配置 如果指定了 在bean上就不需要指定表结构 他会自动拼接</p>

    </div>

    
    
    
	<div>
	  
		<div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
	  
	</div>
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/mybayisplus/" rel="tag"># <i class="fa fa-tag"></i>mybayisplus</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/08/09/Pdf%E4%B8%8EBase64%E7%BC%96%E7%A0%81%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2/" rel="prev" title="Pdf与Base64编码之间的转换">
      <i class="fa fa-chevron-left"></i> Pdf与Base64编码之间的转换
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2021/08/16/10%E4%B8%87%E5%AD%97208%E9%81%93Java%E7%BB%8F%E5%85%B8%E9%9D%A2%E8%AF%95%E9%A2%98%E6%80%BB%E7%BB%93(%E9%99%84%E7%AD%94%E6%A1%88%EF%BC%8C%E5%BB%BA%E8%AE%AE%E6%94%B6%E8%97%8F)/" rel="next" title="10万字208道Java经典面试题总结(附答案，建议收藏)">
      10万字208道Java经典面试题总结(附答案，建议收藏) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  
    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/">MybatisPlus入门</a></p>
  <p><span>文章作者:</span><a href="/" title="访问  的个人博客"></a></p>
  <p><span>发布时间:</span>2020年08月09日 - 21:08</p>
  <p><span>最后更新:</span>2021年08月09日 - 21:08</p>
  <p><span>原始链接:</span><a href="/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/" title="MybatisPlus入门">http://xiaobai-123.gitee.io/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://xiaobai-123.gitee.io/blog/2020/08/09/MybatisPlus%E5%85%A5%E9%97%A8/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>



      
  </div>




          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Mybatis-plus"><span class="nav-number">1.</span> <span class="nav-text">Mybatis plus</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE"><span class="nav-number">2.</span> <span class="nav-text">基本配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ORM%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB"><span class="nav-number">3.</span> <span class="nav-text">ORM映射关系</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#target%E4%BD%9C%E7%94%A8%E8%8C%83%E5%9B%B4"><span class="nav-number">4.</span> <span class="nav-text">target作用范围</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B"><span class="nav-number">5.</span> <span class="nav-text">快速开始</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mybatis-plus%E7%9A%84%E5%AD%97%E6%AE%B5%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB"><span class="nav-number">6.</span> <span class="nav-text">mybatis-plus的字段映射关系</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#crud"><span class="nav-number">7.</span> <span class="nav-text">crud</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E5%A1%AB%E5%85%85"><span class="nav-number">7.1.</span> <span class="nav-text">自动填充</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B9%90%E8%A7%82%E9%94%81"><span class="nav-number">7.2.</span> <span class="nav-text">乐观锁</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E6%93%8D%E4%BD%9C"><span class="nav-number">7.3.</span> <span class="nav-text">查询操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="nav-number">7.4.</span> <span class="nav-text">分页查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C"><span class="nav-number">7.5.</span> <span class="nav-text">删除操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%80%BB%E8%BE%91%E5%88%A0%E9%99%A4"><span class="nav-number">7.6.</span> <span class="nav-text">逻辑删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E6%8F%92%E4%BB%B6"><span class="nav-number">7.7.</span> <span class="nav-text">性能分析插件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8-%E5%88%86%E9%A1%B5-%E6%A8%A1%E7%B3%8A-%E8%87%AA%E5%AE%9A%E4%B9%89SQL"><span class="nav-number">8.</span> <span class="nav-text">多表 分页 模糊 自定义SQL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8-%E5%88%86%E9%A1%B5-%E6%A8%A1%E7%B3%8A"><span class="nav-number">9.</span> <span class="nav-text">多表+分页+模糊</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">10.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mp%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">11.</span> <span class="nav-text">mp的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8"><span class="nav-number">11.1.</span> <span class="nav-text">1.快速入门</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E4%BC%A0%E9%80%9A%E7%9A%84%E6%A8%A1%E5%BC%8F%E5%92%8Cmapper%E7%9A%84%E6%96%B0%E5%A2%9E%E6%96%B9%E6%B3%95"><span class="nav-number">11.2.</span> <span class="nav-text">2.传通的模式和mapper的新增方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="nav-number">11.3.</span> <span class="nav-text">3.常用注解</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E6%8E%92%E9%99%A4%E9%9D%9E%E8%A1%A8%E7%9A%84%E5%AD%97%E6%AE%B5%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-number">11.4.</span> <span class="nav-text">4.排除非表的字段三种方式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-%E6%9F%A5%E8%AF%A2"><span class="nav-number">11.5.</span> <span class="nav-text">5 .查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-%E6%9D%A1%E4%BB%B6%E6%9E%84%E9%80%A0%E5%99%A8"><span class="nav-number">11.6.</span> <span class="nav-text">6.条件构造器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-%E6%9F%A5%E8%AF%A2%E6%8C%87%E5%AE%9A%E5%AD%97%E6%AE%B5"><span class="nav-number">11.7.</span> <span class="nav-text">7.查询指定字段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#9-%E5%AE%9E%E4%BD%93%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E6%9E%84%E9%80%A0%E5%99%A8%E7%9A%84%E5%8F%82%E6%95%B0"><span class="nav-number">11.8.</span> <span class="nav-text">9.实体对象作为构造器的参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#10-allEq%E7%94%A8%E6%88%B7"><span class="nav-number">11.9.</span> <span class="nav-text">10.allEq用户</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#13-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="nav-number">11.10.</span> <span class="nav-text">13.分页查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#14-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E9%A1%B5sql"><span class="nav-number">11.11.</span> <span class="nav-text">14 自定义分页sql</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#14-%E6%9B%B4%E6%96%B0%E5%92%8C%E5%88%A0%E9%99%A4"><span class="nav-number">11.12.</span> <span class="nav-text">14.更新和删除</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#15-AR%E6%A8%A1%E5%BC%8F"><span class="nav-number">11.13.</span> <span class="nav-text">15.AR模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#16-%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE"><span class="nav-number">11.14.</span> <span class="nav-text">16.需要配置</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->
	
      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="小白"
      src="http://xiaobai-123.gitee.io/images/images/1.jpg">
  <p class="site-author-name" itemprop="name">小白</p>
  <div class="site-description" itemprop="description">欢迎观看</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">11</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/L_xioabai_L" title="GitHub → https:&#x2F;&#x2F;blog.csdn.net&#x2F;L_xioabai_L" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/L_xioabai_L" title="E-Mail → https:&#x2F;&#x2F;blog.csdn.net&#x2F;L_xioabai_L" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/L_xioabai_L" title="Weibo → https:&#x2F;&#x2F;blog.csdn.net&#x2F;L_xioabai_L" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/L_xioabai_L" title="Google → https:&#x2F;&#x2F;blog.csdn.net&#x2F;L_xioabai_L" rel="noopener" target="_blank"><i class="fab fa-google fa-fw"></i>Google</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://blog.csdn.net/L_xioabai_L" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;L_xioabai_L" rel="noopener" target="_blank">CSDN</a>
        </li>
    </ul>
  </div>

		<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1297750769&auto=1&height=66"></iframe>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">小白</span>
</div>
<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>
-->

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>

<sapn class="post-meta-divider">|</sapn>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共68.9k字</span>
</div>




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








		  <div ><div id="days"></div>
<script>
function show_date_time(){
    window.setTimeout("show_date_time()", 1000);
    BirthDay=new Date("08/05/2021 15:13:14");
    today=new Date();
    timeold=(today.getTime()-BirthDay.getTime());
    sectimeold=timeold/1000
    secondsold=Math.floor(sectimeold);
    msPerDay=24*60*60*1000
    e_daysold=timeold/msPerDay
    daysold=Math.floor(e_daysold);
    e_hrsold=(e_daysold-daysold)*24;
    hrsold=setzero(Math.floor(e_hrsold));
    e_minsold=(e_hrsold-hrsold)*60;
    minsold=setzero(Math.floor((e_hrsold-hrsold)*60));
    seconds=setzero(Math.floor((e_minsold-minsold)*60));
    document.getElementById('days').innerHTML="已运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
}
function setzero(i) {
    if (i<10) {
        i="0" + i
    };
    return i;
}
show_date_time();
</script></div>
      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>



<!-- 动态背景 -->
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>


  




  
<script src="/blog/js/local-search.js"></script>













  

  



<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>

</html>


